type 'int' is not a subtype of type 'List<dynamic>'

398 Views Asked by At

I have a syncfusion chart. I want to fetch data from firebase. The data is 'int' type on yAxis. And the data for xAxis si just type 'datetime' and not fetch from anywhere. Can anybody help me? I just tired for trying this scince february and nothing happen with my chart. Thank you!

this is my code :

  @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;
}

and this is the error:

════════ 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>'
1

There are 1 best solutions below

0
On

It is difficult to completely understand the code without knowing the data that you are retrieving from Firebase. However, it looks like the offending line is:

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

Is this value truly a List or is it int which the error message implies?