0

Pls help me out in this, In below code I am trying to call a list of hobbies which is stored in firestore but getting error which is mentioned below and neither I am able to fetch values from firestore as I tried to print it. This is a screen shot from firestore:- enter image description here

Getting this error:-

 Unhandled Exception: type 'List<dynamic>' is not a subtype of type 'Future<dynamic>'

This is the screen:-

  class EditInfo extends StatefulWidget{
  final User currentUser;
  EditInfo({this.currentUser});
  @override
 EditInfoState createState () => EditInfoState();

}


class EditInfoState extends State<EditInfo>
with SingleTickerProviderStateMixin{
 
 String currentuser;
 Future myInterest;

 void initState() {
 super.initState();
  getalldata()
}

   getalldata() async {
   Future<CreateAccountData> getUser() async {
   final User user = auth.currentUser;
   return _reference.doc(user.uid).get().then((m) =>  CreateAccountData.fromDocument(m));
   }
  getUser().then((value)async{
   accountData= value;
   DocumentSnapshot doc = await   usercollection.doc(auth.currentUser.uid).get();
    myInterest = doc.data()['hobbies']; //Error takes me here

 });

}

This is where I want to display it in Grid view:-

      child: FutureBuilder(
                      future: myInterest,
                      builder: (BuildContext context,AsyncSnapshot snapshot){
                        if(!snapshot.hasData){
                          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: snapshot.data.docs.length,
                            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.documents[index]['hobbies']),
                              );
                            }
                        );

                      },
                    ),
GAGAN SINGH
  • 261
  • 2
  • 17
  • This is the second time you post this error and still you did not get. `getalldata()` is a Future. Look here https://dart.dev/guides/language/language-tour#handling-futures. You invoke it as synchronous function thats why you have no data. And you missed semicolon in initState, so I dont get how you could even ran this to test out. – Simon Sot May 25 '21 at 09:07
  • yes but that got resolved and I am trying the same way here but I dont know when I go and start working in new screen what happens . This happens with me almost all the time and believe me I getting tired of this same issue. Well, but any way, still I appreciate u commented on this. – GAGAN SINGH May 25 '21 at 09:13
  • so if you put `async` and `await` here `void initState() async { super.initState(); await getalldata(); }` problem persist? – Simon Sot May 25 '21 at 09:15
  • then I get this:-State.initState() must be a void method without an `async` keyword. Rather than awaiting on asynchronous work directly inside of initState, call a separate method to do this work without awaiting it. – GAGAN SINGH May 25 '21 at 09:23
  • true, try one of this solutions https://stackoverflow.com/a/51901311/13701546 – Simon Sot May 25 '21 at 09:30
  • updated, now with different error. – GAGAN SINGH May 25 '21 at 10:45

1 Answers1

0

Your issue is comming from this part of the code:

FirebaseAuth.instance.currentUser.uid

You need to make sure that there is a currentUser. That data is loaded asynchronously so it can happen that it has the value null.

Try to use the onAuthStateChanges listener in your app root code to show only other parts depending on the currentUser if there is one:

FirebaseAuth.instance
  .authStateChanges()
  .listen((User user) {
    if (user == null) {
      print('User is currently signed out!');
    } else {
      print('User is signed in!');
    }
  });

Tarik Huber
  • 7,061
  • 2
  • 12
  • 18
  • but other data is displaying fine like images and name and that too in same screen – GAGAN SINGH May 25 '21 at 08:44
  • `FirebaseAuth.instance.currentUser.uid` is the only code that uses `uid` so the error from the snippeds you shared with us can only come from there. If you have more code then pls show us that to. – Tarik Huber May 25 '21 at 08:54
  • Pls post other issues to other StackOerflow Questions. If the inital one is resolved mark it as such. Continuing on the same question with a different one would make StackOverflow not usable. I know you still get errors but that is our developers life to fix one after another -.- – Tarik Huber May 25 '21 at 11:51
  • If you are sitting to long on that error make a break. Do something else. Go outside. I soled the most difficult errors only after such breaks. Sometimes we digg so deep into problems we don't see the solutions around us. Belive me. That helps more than StackOveflow :) – Tarik Huber May 25 '21 at 11:55
  • sure thing, and I dont get sometimes it works fine in one screen and give errors on other. – GAGAN SINGH May 25 '21 at 11:57