hunterp
hunterp

Reputation: 15986

.once() never fires whenComplete() in flutter using realtimedatabase

In flutter web,

"In then" is never called. I have tried this with adding an async and await to the kDatabase call, and calling it without async. The whenComplete block is never called either.

What could possibly be happening? No errors are ever caught. Any help or perspective would be greatly appreciated.

 void getpostDetail(String? postID) {
    print("Before try");
    try {
        print("In try 1");
        assert(postID != null);
        print("In try 2");
        kDatabase
            .child('post')
            .child(postID!)
            .once()
            .then((DatabaseEvent event) {
          print("In then");
        }).catchError((e, stackTrace) {
          print("in catchError");
        }).whenComplete(() => print("In whenComplete"));
      }
    } catch (error) {
      print("In error");
    } finally {
      print("In finally");
    }
  }

UPDATE: I enabled logs via:

FirebaseDatabase fd = FirebaseDatabase.instance;
fd.setLoggingEnabled(true);
DatabaseReference dr = fd.ref();
dr.child('post').child
.child(postID!)
.once()
.then

And I see

03:36:33.833Z","description":"lets
repl","lanCode":"auto","parentkey":"-MxvCNmiJ9lRNX8XkwL4","retweetCount":0,"user":{"displayName":"Hunte
r","isVerified":false,"profilePic":"https://encrypted-tbn0.gstatic.com/images?q=tbn:ANd9GcRzDG366qY7vXN
2yng09wb517WTWqp-oua-mMsAoCadtncPybfQ&s","userId":"va9C2IQ4ITf1zzfhKQvd7cZdncl2","userName":"@Hunterva9
c"},"userId":"va9C2IQ4ITf1zzfhKQvd7cZdncl2"}

And "In finally" gets called. But "In then","in catchError","In whenComplete" are never called and I don't have my data available where I need it.

Upvotes: 0

Views: 260

Answers (1)

Frank van Puffelen
Frank van Puffelen

Reputation: 599766

I tried reproducing the problem on Chrome, but am unable to. My code:

print("Before try");
try {
  print("In try 1");
  print("In try 2");
  database.ref('71163140/chats')
      .child('oF1b6J4Hz3NGzRb9RmSVFGJdcYi1')
      .once()
      .then((DatabaseEvent event) {
    print("In then");
  }).catchError((e, stackTrace) {
    print("in catchError");
  }).whenComplete(() => print("In whenComplete"));
} catch (error) {
  print("In catch");
} finally {
  print("In finally");
}

The ouput:

Before try

In try 1

In try 2

In finally

In then

In whenComplete

Even when I try to read a non-existing node that is the flow, which makes sense as reading a non-existing node is not an error.

The only way I can get it to fail is by adding FirebaseDatabase.instance.goOffline() before the code, in which case the output becomes:

Error: [firebase_database/unknown] Error: Client is offline.

So I'm not sure what is going on in your case. You might want to see if changing the library version makes any difference, but I don't think there were any changes in this area recently.

Upvotes: 1

Related Questions