Retrieve Data from Firestore and Pass it to a variable. Flutter

Viewed 358

Hi i would like to ask how can I retrieve the data from the Firestore and send it to a double.

This is the code where I retrieve the data from Firestore.

Firestore.instance
            .collection('locations')
            .snapshots()
            .listen((driverLocation){
          driverLocation.documents.forEach((dLocation){
            dLocation.data['Latitude'] = latitude;
            dLocation.data['Longitude'] = longitude;
            print(latitude);
          });
        });

I store it inside the dLocation and when i print(dLocation.data) it will display the latitude and longitude in the Firestore. But when i pass it to the double latitude and double longitude it returns null.

busStop.add(
          Marker(
            markerId: MarkerId('driverLocation')
            ,
            draggable: false,
            icon: BitmapDescriptor.defaultMarker,
            onTap: () {
            },
            position: LatLng(latitude, longitude),
          ));

Then i would like to pass the data that is in the double latitude and double longitude into the marker so that the marker will move accordingly to the latitude and longitude in the Firestore.

Everything that is happening here is in a initState().

**If theres anything you would want to ask please feel free to do so as i do not have any idea on how to convey my question. Thank you so much in advance.

1 Answers

You're doing it in the wrong way. Right now you are assigning the value of latitude (which is null) to the value of dLocation.data['latitude']. What you want to do is this:

latitude = dLocation.data['latitude'];
longitude = dLocation.data['longitude'];

with this change, the value of dLocation.data['latitude'] will be assigned to latitude and the value of dLocation.data['longitude'] will be assigned to longitude variable

Update:

To get new markers and show them on the screen with latitude and longitude values, you can do something like this:

@override
void initState(){
Firestore.instance
            .collection('locations')
            .snapshots()
            .listen((driverLocation){
          //busStop = []; removes all the old markers and you don't get duplicate markers with different coordinates
          driverLocation.documents.forEach((dLocation){
            dLocation.data['Latitude'] = latitude;
            dLocation.data['Longitude'] = longitude;
            busStop.add(
               Marker(
                  markerId: MarkerId('driverLocation')
                  ,
                  draggable: false,
                  icon: BitmapDescriptor.defaultMarker,
                  onTap: () {
                  },
                  position: LatLng(latitude, longitude),
          )); //you need to check for duplicate markers here. you can do it by giving each marker an unique id and check for the same marker in the list so you don't get duplicate markers.
          });
         setState((){}); //rebuilds the widget tree after adding the markers to the busStop list
        });

}

What's happening here is you add the markers to the busStop list and after adding all the markers, you call setState and the widget tree rebuilds the screen with the latest data. You might need to check for duplicate markers because they might be re-added to the busStop list. Or you can simply remove all the old markers and add the new ones by using busStop = []; before adding to busStop

Related