0

I am trying to display the list of hobbies of the user from firestore and I get the value from there as it gets print in console but not able to display it.

Error:-

    The getter 'hobbies' was called on null.
    Receiver: null
    Tried calling: hobbies 

Create account data is model class:-

   Future<CreateAccountData> getUser() async {
   final User user = auth.currentUser;
  return _reference.doc(user.uid).get().then((m) =>   CreateAccountData.fromDocument(m));
    }

getting data this way and it gets print also in console:-

 List hobbies;

 void initState() {
 super.initState();
 getUser().then((value) {
  if (!mounted) return;
  setState(() {
    
      accountData = value;
      
      hobbies = value.hobbies;
      print("hobbies"+ hobbies.toString());


  });
});
}

Error takes me to the line I commented:-

           child:GridView.builder(
                   physics: BouncingScrollPhysics(),
                   scrollDirection: Axis.horizontal,
                   shrinkWrap: true,
                   itemCount: accountData.hobbies.length, // Error take me to hobbies.
                        gridDelegate: SliverGridDelegateWithFixedCrossAxisCount(crossAxisCount: 2, childAspectRatio: 5, crossAxisSpacing: 5,),
                        itemBuilder: (BuildContext context, int index){
                          
                          return Padding(
                            padding: EdgeInsets.fromLTRB(5, 5, 5, 5),
                            child: Text(accountData.hobbies[index]),
                          );
                        }
                    ), 

On suggestions updated to this(wrapped in future builder):-

         child: FutureBuilder(
                      future: getUser(),
                      builder: (context, snapshot){
                        if(snapshot.connectionState == ConnectionState.waiting){
                          return Center(
                            child: CircularProgressIndicator(),
                          );
                        } if(snapshot.data.docs.isEmpty){
                          return Align(
                            alignment: FractionalOffset.centerLeft,
                            child: Text("Add what you love to do.....",textAlign: TextAlign.left,style: TextStyle(fontSize: 17),),
                          );
                        }
                          return GridView.builder(
                              physics: BouncingScrollPhysics(),
                              scrollDirection: Axis.horizontal,
                              shrinkWrap: true,
                              itemCount: accountData?.hobbies?.length ?? 0,
                              gridDelegate: SliverGridDelegateWithFixedCrossAxisCount(
                                crossAxisCount: 2,
                                childAspectRatio: 5,
                                crossAxisSpacing: 5,),
                              itemBuilder: (BuildContext context,
                                  int index) {
                                 return Padding(
                                  padding: EdgeInsets.fromLTRB(5, 5, 5, 5),
                                  child: Text(accountData.hobbies[index]),
                                );
                              }
                          );
                        },
                    ),

Now getting NoSuchMethodError error:-

      Class 'CreateAccountData' has no instance getter 'docs'.
      Receiver: Instance of 'CreateAccountData'
      Tried calling: docs
GAGAN SINGH
  • 261
  • 2
  • 17
  • Does this answer your question? [What is a NoSuchMethod error and how do I fix it?](https://stackoverflow.com/questions/64049102/what-is-a-nosuchmethod-error-and-how-do-i-fix-it) – nvoigt May 26 '21 at 05:13
  • thanks people:- resolved it , no need to bother now. I called it directly instead of calling it grid view. child:Text(hobbies), but still don't get what was wrong when called in grid view. anyway. done now. – GAGAN SINGH May 26 '21 at 07:35

3 Answers3

0

check hobbies is null

 itemCount: accountData?.hobbies?.length ?? 0, 
Nidheesh MT
  • 1,074
  • 11
  • 18
0

In flutter 2.0, we have null safety. We need to check if value is null or not using an ! mark after the variable. I will post the code in a few minutes since Im in school rn

Siddharth Agrawal
  • 2,960
  • 3
  • 16
  • 37
0

Wrap GridView.builder in a FutureBuilder passing getUser() as the future.

child: FutureBuilder(
  future: getUser(),
  builder: (context, snapshot) {
    if (snapshot.connectionState == ConnectionState.waiting)
      return CircularProgressIndicator();
    if (snapshot.hasData)
      return GridView.builder(
          physics: BouncingScrollPhysics(),
          scrollDirection: Axis.horizontal,
          shrinkWrap: true,
          itemCount:
              snapshot.data.hobbies.length, // Error take me to hobbies.
          gridDelegate: SliverGridDelegateWithFixedCrossAxisCount(
            crossAxisCount: 2,
            childAspectRatio: 5,
            crossAxisSpacing: 5,
          ),
          itemBuilder: (BuildContext context, int index) {
            //DocumentSnapshot interestList = snapshot.data.docs[index]['hobbies'];
            return Padding(
              padding: EdgeInsets.fromLTRB(5, 5, 5, 5),
              child: Text(snapshot.data.hobbies[index]),
            );
          });
    return Text('No data.');
  },
),
Lee3
  • 2,882
  • 1
  • 11
  • 19
  • tried this and now getting this error:- Class 'CreateAccountData' has no instance getter 'docs'. Receiver: Instance of 'CreateAccountData' Tried calling: docs – GAGAN SINGH May 26 '21 at 05:06
  • This issue is elsewhere in your code. You will need to share the parts of the `CreateAccountData` class that try to access `docs` – Lee3 May 26 '21 at 05:09
  • Please share the `CreateAccountData.fromDocument()` constructor definition. Are you accessing `docs` there? If not, where are you attempting to access a `docs` variable from the `CreateAccountData` class? – Lee3 May 26 '21 at 07:21
  • Also, what kind of object is `_reference`? Is it a Firebase 'CollectionReference'? – Lee3 May 26 '21 at 07:23
  • hey, done resolved it finally. instead of calling it in grid view calling it in simple way :- child Text(hobbies), I don't know what is happening when call it in grid view. anyway thanks. – GAGAN SINGH May 26 '21 at 07:32