randomstudent
randomstudent

Reputation: 73

Data that is saved in a Snapshot and transfer it to a double

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 get 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().

Upvotes: 0

Views: 128

Answers (2)

jgithaiga
jgithaiga

Reputation: 134

Try this:

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

Upvotes: 0

Merym
Merym

Reputation: 881

you have reversed the assignment

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

Upvotes: 1

Related Questions