¿How can I iterate a snapshot in Flutter?

512

I believe passing,bmisnapshot.data should solve your error instead of passing bmisnapshot,

 return Container(
                    child: BMITimeChart(
                  data: data(bmisnapshot.data),
              ),);

Also always initialize your list if you are going use list methods, else you will get a NoSuchMethodError as it is called on null;

List bmiDataList = [];

And I'm unsure if this is right either,

    value: double.parse(rawData.value['value']),
          xAxis: DateTime.parse(rawData.value['date'])));

as AynscSnapshot has no value attribute, and since you're already passing in the data, you should be able to extract the values like this

    value: double.parse(rawData['value']),
          xAxis: DateTime.parse(rawData['date'])));
Share:
512
Rodrigo Isaac Guzmán
Author by

Rodrigo Isaac Guzmán

Updated on December 24, 2022

Comments

  • Rodrigo Isaac Guzmán
    Rodrigo Isaac Guzmán over 1 year

    I'm building a flutter app, and I'm trying to create a Time Chart based on a value obtained with a calculator, so I created a database with sqflite and each element in the db has an Id, a date, and a value.

    import 'package:bezier_chart/bezier_chart.dart';
    import 'package:flutter/material.dart';
    import 'package:mi_working_app/components/bmi_chart.dart';
    import 'package:mi_working_app/services/statistics_bmi_dbhelper.dart';
    
    class StatisticsWidget extends StatefulWidget {
      @override
      _StatisticsWidgetState createState() => _StatisticsWidgetState();
    }
    
    class _StatisticsWidgetState extends State<StatisticsWidget> {
      BMIDbHelper bmihelper;
    
      @override
      void initState() {
        super.initState();
        bmihelper = BMIDbHelper();
      }
    
      @override
      Widget build(BuildContext context) {
        return Column(
          children: [
            FutureBuilder(
                future: bmihelper.getBMIs(),
                builder: (BuildContext context, AsyncSnapshot bmisnapshot) {
                  if (bmisnapshot.hasData) {
                    print(bmisnapshot.data);
                    print(bmisnapshot.data.length);
                    return Container(
                        child: BMITimeChart(
                      data: data(bmisnapshot),
                    ));
                  } else {
                    return Center(
                      child: CircularProgressIndicator(),
                    );
                  }
                }),
          ],
        );
      }
    
    }
    

    Now to create the chart, I need to extract the date and value from each element, and for that I did this:

    List<DataPoint<dynamic>> data(rawData) {
        List bmiDataList;
    
        for (Map m in rawData) {
          bmiDataList.add(DataPoint<DateTime>(
              value: double.parse(rawData.value['value']),
              xAxis: DateTime.parse(rawData.value['date'])));
        }
    
        print(bmiDataList);
        return bmiDataList;
      }
    

    Now I'm getting the "type 'AsyncSnapshot<List>' is not a subtype of type 'Iterable'". I understand the error, I can't Iterate a snapshot, but I don't know how else can I retrieve the data

    • Atul Chaudhary
      Atul Chaudhary over 3 years
      can you please share the error code with us for better understanding