简体   繁体   中英

Flutter The method '[]' was called on null error

I have a Flutter app with Firebase backend. I am using email and google sign in method. When user sign in for the first time using google sign in method, this wierd error pops up just for a second. (When user register using email/pswrd, this doesnt appear)

The following NoSuchMethodError was thrown building StreamBuilder(dirty, state: _StreamBuilderBaseState>#e1ffb): The method '[]' was called on null. Receiver: null Tried calling:

Here is my code on github: https://github.com/TenPetr/fridgy (this error is probably caused from lib/pages/home.dart file)

Thanks for your answers!

This happens because when you Stream is in its loading state, the way to solve this is to use the ConnectionState of the stream and show either a ProgressBar or a empty container.

//Data Logic
Stream<QuerySnapshot> snapshot =  Firestore.instance.collection("collection_name").snapshots();

//Ui Logic
StreamBuilder(
  stream: snapshot,
  builder: (BuildContext context, AsyncSnapshot<QuerySnapshot> snapshot) {
    if(snapshot.connectionState == ConnectionState.active) {
      //Further Logic
      return Text('It works');
    } else {
      return CircularProgressIndicator();
    }
  },
);

I found that the error is timing due to the user @Preet Parekh. And I solve this issue by removing method that was responsible for determine if user is logged for the first time or not. Instead of this, I used propery on AuthResult - result.additionalUserInfo.isNewUser

https://firebase.google.com/docs/reference/android/com/google/firebase/auth/AdditionalUserInfo

Thanks for your help.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM