Unable to find if a node exists in realtime database

0

enter image description here

I have a firebase realtime tree which has a particular node New Ride set when the user is online, when offline the node New Ride gets removed. This works ok for the first time, When the user tries to go online again I can't set up the node. I want to check if the node already exists in the tree if not add it.

This is my code so far

DatabaseReference rideRequestRef = FirebaseDatabase(databaseURL: firebaseUrl)
    .reference()
    .child("Drivers")
    .child(currentFirebaseUser.uid)
    .child("New Ride");
var ref = FirebaseDatabase(databaseURL: firebaseUrl)
        .reference()
        .child("Drivers")
        .child(currentFirebaseUser.uid);

    await ref
        //.child(currentFirebaseUser.uid)
        .child("New Ride")
        .once()
        .then((DataSnapshot dataSnapshot) {
      if (dataSnapshot == null) {
       
        rideRequestRef.set("searching");
      } else {
        rideRequestRef.set("searching");
       
      }
    });
   
    rideRequestRef.onValue.listen((event) {});
  }
firebase-realtime-database flutter
2021-11-24 06:44:20
1

0

Given your code here:

await ref
    //.child(currentFirebaseUser.uid)
    .child("New Ride")
    .once()
    .then((DataSnapshot dataSnapshot) {
  if (dataSnapshot == null) {       
    rideRequestRef.set("searching");
  } else {
    rideRequestRef.set("searching");       
  }
});

It seems that whether the node exists or not makes no difference on what you do. So why not replace this entire fragment with this?

rideRequestRef.set("searching");       
2021-11-24 15:49:56

In other languages

This page is in other languages

Русский
..................................................................................................................
Italiano
..................................................................................................................
Polski
..................................................................................................................
Română
..................................................................................................................
한국어
..................................................................................................................
हिन्दी
..................................................................................................................
Français
..................................................................................................................
Türk
..................................................................................................................
Česk
..................................................................................................................
Português
..................................................................................................................
ไทย
..................................................................................................................
中文
..................................................................................................................
Español
..................................................................................................................
Slovenský
..................................................................................................................