1

我有一个同步融合图表。我想从 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>'
4

1 回答 1

0

如果不知道您从 Firebase 检索的数据,就很难完全理解代码。但是,看起来有问题的行是:

List<dynamic> values = snapshot.data.snapshot.value["Moisture"];

这个值是真的 aList还是int错误消息所暗示的?

于 2021-04-21T10:55:56.787 回答