我有一个同步融合图表。我想从 firebase 获取数据。yAxis 上的数据是“int”类型。xAxis si 的数据只需键入“日期时间”,而不是从任何地方获取。有谁能够帮我?我只是厌倦了尝试这个 2 月的科学,我的图表没有任何反应。谢谢!
这是我的代码:
@override
Widget build(BuildContext context) {
return MaterialApp(debugShowCheckedModeBanner: false, home: LiveChart());
}
}
class LiveChart extends StatefulWidget {
@override
_LiveChartState createState() => _LiveChartState();
}
class _LiveChartState extends State<LiveChart> {
Timer timer;
int count = 0;
final dbRef = FirebaseDatabase.instance.reference();
List<_ChartData> chartData = <_ChartData>[];
Map<dynamic, dynamic> data = new Map();
void _updateDataSource(Timer timer) {
setState(() {
if (count >= 59) {
count = 0;
}
chartData.add(_ChartData(x: DateTime(2021, 1, 1, count), y1: data['y1']));
if (chartData.length == 20) {
chartData.removeAt(0);
}
count = count + 1;
});
}
@override
void initState() {
super.initState();
}
@override
Widget build(BuildContext context) {
return Scaffold(body: _showChart());
}
Widget _showChart() {
return StreamBuilder(
stream: dbRef.child("Data").orderByChild('$count').onValue,
builder: (context, snapshot) {
Widget widget;
if (snapshot.hasData &&
!snapshot.hasError &&
snapshot.data.snapshot.value != null) {
List<dynamic> values = snapshot.data.snapshot.value["Moisture"];
if (values != null && count < values.length) {
data = values[count];
timer = Timer.periodic(
Duration(milliseconds: 1000), _updateDataSource);
}
widget = Container(
child: SfCartesianChart(
tooltipBehavior: TooltipBehavior(enable: true),
primaryXAxis: DateTimeAxis(),
series: <LineSeries<_ChartData, DateTime>>[
LineSeries<_ChartData, DateTime>(
dataSource: chartData,
xValueMapper: (_ChartData data, _) => data.x,
yValueMapper: (_ChartData data, _) => data.y1,
)
],
),
);
} else {
widget = Center(child: CircularProgressIndicator());
}
return widget;
});
}
@override
void dispose() {
super.dispose();
timer?.cancel();
}
}
class _ChartData {
_ChartData({this.x, this.y1});
final DateTime x;
final int y1;
}
这是错误:
════════ Exception caught by widgets library ═══════════════════════════════════
The following _TypeError was thrown building StreamBuilder<Event>(dirty, state: _StreamBuilderBaseState<Event, AsyncSnapshot<Event>>#ec6fc):
type 'int' is not a subtype of type 'List<dynamic>'