简体   繁体   中英

fetching data from firebase real time database

在此处输入图像描述

This is how my data is saved inside LiveTracker there is 2,8 which are bus route id and inside it there is auto generated firebase id and inside that id there is latitude and longitude and i wanna access those latitude and longitude but i am stuck at here and dont know how to fetch that data need soo help

This is my data format which i get when i print data

{-NCQE5NU7ajW2L57gt1o: {latitude: -122.07730612330596, longitude: 37.41749912586621}, -NCQF6O3vx2NYVdZKD0X: {latitude: -122.083922, longitude: 37.4214938}, -NCQF6kWJPOyLuymJeXz: {latitude: -122.07730453282453, longitude: 37.417496832888055}, -NCQF5afZXRqg1Heb9Oq: {latitude: -122.07730562660478, longitude: 37.417495975114676}, -NCQDoU67-ZjN4kNEyPr: {latitude: -122.07730474508638, longitude: 37.41749751091001},

this is how i am trying to fetch that latitude and longitude but i am stuck and dont know how to get it

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

data appears to be a Map object when you print it. Assuming your app code is aware of the autogenerated IDs, you can fetch latitude and longitude like so:

var data = event.snapshot.value;
var latitude = data['-NCQE5NU7ajW2L57gt1o']['latitude'];
var longitude = data['-NCQE5NU7ajW2L57gt1o']['longitude'];
print("$latitude, $longitude");

If I misunderstood and your issue is not being aware of the auto generated IDs, then you need to either restructure your data or fetch the keys at runtime with: data.keys.toList()

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM