GAGAN SINGH
GAGAN SINGH

Reputation: 281

Error:- type 'List<dynamic>' is not a subtype of type 'Future<dynamic>'

How can I get away from this error or what else I can call instead of List dynamic or Future dynamic or how can I convert List into Future?I just want to display this text from firestore.enter image description here Error:-

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

Getting data this way and commented line which takes me to error:-

    class EditInfo extends StatefulWidget{
    @override
   EditInfoState createState () => EditInfoState();
   }


  class EditInfoState extends State<EditInfo>
 with SingleTickerProviderStateMixin{
 final CollectionReference _reference =   FirebaseFirestore.instance.collection("users");
 final FirebaseAuth auth = FirebaseAuth.instance;
 CreateAccountData accountData;

 Future myInterest;


    
    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 to,myInterest

     });
 }

Displaying 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']),
                              );
                            }
                        );

                      },
                    ),

Upvotes: 1

Views: 2449

Answers (1)

Ced
Ced

Reputation: 17417

You are getting this error because you are assigning a List to a variable declared as Future.

You are declaring:

 Future myInterest;

and then setting a value that is not a future to it:

myInterest = doc.data()['hobbies'];

Ultimately there is a lot of way to resolve this, one of which:

Future getAllData() async {
  // do the api stuff
  return doc.data()['hobbies'];
};

Then assign to it:

myInterest = getAllData();

I encourage you to learn about futures though.

Upvotes: 1

Related Questions