How to check if node exists in Firebase Database with Flutter/Dart

I've seen this question answered multiple times already for JavaScript and other languages. There, it always comes down to get a snapshot and use a method called exists() to check. But in Dart/Flutter, there is no such method. Here's what I have for now:

devicesRef.child(deviceId).once().then((DataSnapshot data) {
    print(data.key);
    print(data.value);        
});

I want to check whether a node called deviceId already exists.

So how can I check if a node exists in Firebase Realtime Database with Dart/Flutter?


Solution 1:

I would guess, since there is no such thing as a null child value in Realtime Database, that you could simply check if data.value is null.

Solution 2:

Pass Your DatabaseReference to this method , and will return true if it Exists

 Future<bool> rootFirebaseIsExists(DatabaseReference databaseReference) async{
    DataSnapshot snapshot = await databaseReference.once();

    return snapshot !=null;
  }