1
votes

I am making a profile page on flutter with firebase firestore and I received no such method error, the method '[]' was called on null and Tried calling:. Below is my User class which I named "Kisi"

 class Kisi {
  final String username,uid,email,photoUrl,name,surname;
  Kisi({this.username,this.uid,this.email,this.name,this.photoUrl,this.surname});

  factory Kisi.fromDocument(DocumentSnapshot doc)
  {
    return Kisi(
      uid:  doc.data()['uid'],
      username:  doc.data()['username'],
      name:  doc.data()['name'],
      photoUrl:  doc.data()['photoURL'],
      surname:  doc.data()['surname']
    );
  }
}

I get the error in the below class in the build method where I call the FirebaseFirestore.

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

class MyAccount extends StatefulWidget {
  @override
  _MyAccountState createState() => _MyAccountState();
}

class _MyAccountState extends State<MyAccount> {
  FirebaseAuth firebaseAuth = FirebaseAuth.instance;
  User currentUser;

  @override
  void initState() {
    super.initState();
    _loadCurrentUser();
  }

  void _loadCurrentUser() {
    firebaseAuth.authStateChanges().listen((User user) {
      setState(() {
        this.currentUser = user;
      });
    });
  }

  @override
  Widget build(BuildContext context) {
    return StreamBuilder(
      stream: FirebaseFirestore.instance
          .collection('Users')
          .doc(currentUser.uid.toString())
          .snapshots(),
      builder: (context, snapshot) {
        Kisi user = Kisi.fromDocument(snapshot.data);
        return Scaffold(
          appBar: AppBar(
            elevation: 0,
          ),
          body: Padding(
            padding: const EdgeInsets.all(15.0),
            child: ListView(
              children: <Widget>[
                Container(
                  child: Text(
                    "Giriş ve Güvenlik",
                    style:
                        TextStyle(fontSize: 28.0, fontWeight: FontWeight.bold),
                  ),
                ),
                Card(
                  child: Column(
                    children: <Widget>[
                      Container(                      
                        child: Text(
                          "İsim:",
                          style: TextStyle(fontWeight: FontWeight.bold),), ),
                      Container(                     
                        child: Text(
                          user.name),
                      ),                
                    ],
                  ),
                ),
              ],
            ),
          ),
        );
      },
    );
  }
}
1

1 Answers

1
votes

StreamBuilder is asynchronous therefore, it will take time to get the data. You need to do the following:

  @override
  Widget build(BuildContext context) {
    return StreamBuilder(
      stream: FirebaseFirestore.instance
          .collection('Users')
          .doc(currentUser.uid.toString())
          .snapshots(),
      builder: (context, snapshot) {
      if(snapshot.hasData){
        Kisi user = Kisi.fromDocument(snapshot.data);
        return Scaffold(
          appBar: AppBar(
            elevation: 0,
          ),
          body: Padding(
            padding: const EdgeInsets.all(15.0),
            child: ListView(
              children: <Widget>[
                Container(
                  child: Text(
                    "Giriş ve Güvenlik",
                    style:
                        TextStyle(fontSize: 28.0, fontWeight: FontWeight.bold),
                  ),
                ),
                Card(
                  child: Column(
                    children: <Widget>[
                      Container(                      
                        child: Text(
                          "İsim:",
                          style: TextStyle(fontWeight: FontWeight.bold),), ),
                      Container(                     
                        child: Text(
                          user.name),
                      ),                
                    ],
                  ),
                ),
              ],
            ),
          ),
        );
       },
      else {
        return CircularProgressIndicator();
      }
      },
    );
  }

Add a condition, which will show a circularprogressindicator when the data is still not fetched.