James Piner
James Piner

Reputation: 329

How to get firestore data in flutter

My code is getting this error when I call readLocationData() in my initState():

Unhandled Exception: type 'GeoPoint' is not a subtype of type 'Iterable'.

Not sure how to fix this one. My firebase Cloud Firestore has a list of 4 documents, each with a GeoPoint field called geopoint, each with a handcoded latitude and longitude.

  Future<void> readLocationData() async {


    var query = await Firestore.instance.collection('bakeries').getDocuments();

    List<Map<dynamic, dynamic>> mainList = List<Map<dynamic, dynamic>>();

    query.documents.forEach((doc) {

      List<Map<dynamic, dynamic>> values = List.from(doc.data['geopoint']);
      print('geopoint' + values.toString() +'\n');

    });

    for(var i=0; i < mainList.length; i++) {
      Map<dynamic, dynamic> map = mainList[i];
      GeoPoint geopoint = map['geopoint'];
  print(geopoint.latitude.toString()+','+ geopoint.longitude.toString());
  }

  }

Upvotes: 1

Views: 183

Answers (1)

Frank van Puffelen
Frank van Puffelen

Reputation: 598623

The error message indicates that a GeoPoint object is not iterable, which is correct. I'm not sure what you expect List.from(doc.data['geopoint']) to return in that sense.

If you expect the latitude and longitude to be taken from the GeoPoint, you'll have to access those properties explicitly in your code. But event then I'm not sure what the key and value in your Map<dynamic, dynamic> would become.

Upvotes: 1

Related Questions