Search code examples
fluttermarkers

Flutter Map<String, dynamic>: Issues to get data from Firestore


I'm trying the get data from Firestore but I have a issues on Map<String, dynamics>. Any idea how to fix this issues?

    final QuerySnapshot result = await firestore.collection('markers').get();
    final List<DocumentSnapshot> documents = result.docs;
    int count = 0;
    documents.forEach((data) {
    Map<String, dynamic> datos = data.data;
      GeoPoint tmp = datos['position'];
      if (count < int.parse(data.id)) count = int.parse(data.id);
      final MarkerId markerId = MarkerId(data.id);
      LatLng point = LatLng(tmp.latitude, tmp.longitude);
      final Marker marker = Marker(
          markerId: markerId,
          position: point,
          infoWindow: InfoWindow(title: 'Id: ' + data.id),
          onTap: () {
            _onTapped(markerId);
          },
          icon: BitmapDescriptor.defaultMarkerWithHue(
              BitmapDescriptor.hueBlue));
      _markers[markerId] = marker;
    });
    _markerIdCounter = count + 1;
    setState(() {});
  }```

Solution

  •         final QuerySnapshot result = await firestore.collection('markers').get();
            final List<DocumentSnapshot> documents = result.docs;
            int count = 0;
            documents.forEach((data) {
    // I changed this line
            Map<String, dynamic> datos = data.data() as Map<String, dynamic>;
              GeoPoint tmp = datos['position'];
              if (count < int.parse(data.id)) count = int.parse(data.id);
              final MarkerId markerId = MarkerId(data.id);
              LatLng point = LatLng(tmp.latitude, tmp.longitude);
              final Marker marker = Marker(
                  markerId: markerId,
                  position: point,
                  infoWindow: InfoWindow(title: 'Id: ' + data.id),
                  onTap: () {
                    _onTapped(markerId);
                  },
                  icon: BitmapDescriptor.defaultMarkerWithHue(
                      BitmapDescriptor.hueBlue));
              _markers[markerId] = marker;
            });
            _markerIdCounter = count + 1;
            setState(() {});
          }`