accessing data which is stored inside firebase real time database

Viewed 22

enter image description hereI am getting data in this way from firebase real time database

{
  -NCPZuwqiuMwtaKfW-g9: {
    latitude: -122.083922, 
    longitude: 37.4214938
  }, 
  -NCPntX1RFEyzvxYsgBe: {
    latitude: -122.083922, 
    longitude: 37.4214938
  }, 
  -NCPoSmW2MxMnySLDCYg: {
    latitude: -122.07730409759519, 
    longitude: 37.41749643309451
  }, 
  -NCPbS50dJZ_gtlUEK82: {
    latitude: -122.07730527365285, 
    longitude: 37.417498330741125
  },

now i want to access the latitude and longitude of the database but i am unable to get it what i tried is this

_dbReference
            ?.child("LiveTracker")
            .child("$passedId")
            .onValue
            .listen((event) {
          print("asasdasdasdsaddsadsadsadsadsad${event.snapshot.value}");
          /* _markers.add(Marker(
              markerId: MarkerId(DateTime.now().toIso8601String()),
              position: LatLng(double.parse(event.snapshot.value.toString()),
                  double.parse(event.snapshot.value.toString()))));*/
        });

i want to listen to the real time data and add that latitude and longitude inside my marker but i am unable to access the latitude and longitude need some guidance

1 Answers

If LiveTracker is the node right above the data you shared, and passedId is this one of the -N keys, you can get the latitude and longitude with:

var data = event.snapshot.value; // this is a Map<String, Object>
print(data["longitude");
print(data["latitude");

Update: since you have multiple -N... nodes under your passedId key, you'll need to loop over the entries in the map to get the individual longitude and latitude values. For an example of this see Iterate through map values in dart / flutter

Related