Firebase Connection State Always Returning False on First Run

Detecting Connection State Called Twice

What you're seeing is the expected behavior.

The .info/connected flag determines whether the app/client is connected to the Firebase Database backend. While this of course requires that you have an internet connection, there is more to it than that. That's why it is possible for .info/connected to be false even though you have a working internet connection.

This is especially true when you start the app. It takes a few moments after the app starts before the Firebase client is connected to its database server, so usually the .info/connected value starts as false and then becomes true. Sometimes it even toggles a few times before settling.

Also see:

  • How to handle internet connection status Firebase
  • Firebase connection state listener returns false in javascript

My function always returns false the first time

Your method testLogin is incorrect. Method dataTaskWithRequest is asynchronous. You need to create two callbacks success and failure (closures). Which will be called if request completes. Look at this https://github.com/Alamofire/Alamofire.

Flutter StreamBuilder connectionState problem with firestore

connectionState is never equal to true because it's simply not a boolean value.
As you noticed, it's an ConnectionState enum value, so if you want to check a connectionState of AsyncSnapshot, you have to compare it to a ConnectionsState enum value. So for example if you want to know whether stream has loaded you need to check if state is different than waiting: (you have observed it by yourself with print statements)

if(streamSnapshot1.connectionState
!= ConnectionState.waiting) {
//stream has loaded some first values
}


Related Topics



Leave a reply



Submit