Search code examples
flutterdartfirebase-realtime-databasegeofire

I want to Put extra information at realtime database node created using geofire (Flutter)


I want to put extra data at node in database this node is created using geofire and when i try to put this information is appear at database then hide like below,

availableDrivers
   3gDKSbnMEBYZsZDwL6lmiQerP9P2
       g: 
         "stq5ncg7kr"
       l
        0: 30.0688058
        1: 31.2387825

I want to add extra information like

availableDrivers
   3gDKSbnMEBYZsZDwL6lmiQerP9P2
       g: 
         "stq5ncg7kr"
       l
        0: 30.0688058
        1: 31.2387825
      name: bishoy
      age: 24

they added but then hide or removed, below my code.

main


DatabaseReference adminRef = FirebaseDatabase.instance.reference().child("admin");
DatabaseReference driversRef = FirebaseDatabase.instance.reference().child("drivers");
DatabaseReference newRequestRef = FirebaseDatabase.instance.reference().child("Ride Request");
DatabaseReference rideRequestRef = FirebaseDatabase.instance.reference().child("drivers").child(currentfirebaseUser!.uid).child("newRide");
DatabaseReference availableDriver = FirebaseDatabase.instance.reference().child("availableDrivers").child(currentfirebaseUser!.uid);

function that generate availabledriver and I put my extra information their

 void makeDriverOnlineNow() async
  {
    Position position = await Geolocator.getCurrentPosition(desiredAccuracy: LocationAccuracy.high);
    currentPosition = position;

    Map<String, dynamic> driverMap = {
      "name" : driversInformation!.name,
      "phone" : driversInformation!.phone,
    };

    Geofire.initialize("availableDrivers");
    Geofire.setLocation(currentfirebaseUser!.uid, currentPosition!.latitude, currentPosition!.longitude);
    await availableDriver.update(driverMap);

    rideRequestRef.set("searching");
    rideRequestRef.onValue.listen((event) {

    });
  }

void getLocationLiveUpdates()
  {
    homeTabPageStreamSubscription = Geolocator.getPositionStream().listen((Position position)
    {
      currentPosition = position;
      if(isDriverAvailable == true)
      {
        Geofire.setLocation(currentfirebaseUser!.uid, position.latitude, position.longitude);
      }
      LatLng latLng = LatLng(position.latitude, position.longitude);
      newcontrollerGoogleMap!.animateCamera(CameraUpdate.newLatLng(latLng));
    });
  }

Solution

  • It looks like it is not possible in the flutter_geofire to add your own data to the node that it manages. Of the GeoFire implementations that Firebase itself delivers only the Android library allows you to add your own data to the geolocation node, and I'll admit to not being a big fan of it.

    To keep your own data, set up you database like this:

    driverLocations: {
      // geolocation data, managed by geofire
      "3gDKSbnMEBYZsZDwL6lmiQerP9P2": {
        "g": "stq5ncg7kr",
        "l": [30.0688058, 31.2387825]
      }
    },
    drivers: {
      // driver properties, managed by you
      "3gDKSbnMEBYZsZDwL6lmiQerP9P2": {
        name: "bishoy",
        age: 24
      }
      }
    }
    

    Now it is clear what data is managed by whom, and you'll typically only access the driverLocations data through GeoFire. Once you get the key of a driver from GeoFire, you can look up their properties from /drivers/$key. Doing this for multiple drivers is not as slow as you may initially expect, because Firebase pipelines the requests over a single WebSocket connection.