Ziv Cheung
Ziv Cheung

Reputation: 77

type 'Future<dynamic>' is not a subtype of type 'List<Profile>' in type cast

List<Profile> demoProfiles =  getdata() as List<Profile>;

Future getdata() async{
              List<Profile> list=[];
              list = await Firestore.instance
              .collection("users").getDocuments().then((querySnapshot){
               querySnapshot.documents.forEach((document) {
               list.add( Profile(
                    photos: [document['photoUrl'],],
                    name: document['nickname'],
                    age: 2,
                    distance: 2,
                    education: document['photoUrl']

                ));

            });
            return list;
        });
}

class _MainControllerState extends State<MainController> {



 final MatchEngine matchEngine = MatchEngine (

    matches:demoProfiles.map((Profile profile) => Match(profile: profile)).toList()

  );
  @override
  Widget build(BuildContext context) {
    return Container(
      padding: EdgeInsets.fromLTRB(0.0, 60.0, 0.0, 30.0),  
      child: CardStack (
        matchEngine: matchEngine
      ),

    );
  }

}

When I run my code , it returned the error :type 'Future' is not a subtype of type 'List' in type cast. How can I fix it ? it is the matchEngine runs before I get the data from firebase? I tried to add await to List demoProfiles = getdata() as await List; but it returns error. I am really new to flutter and this problem had been nagging at me all day . please help me

enter image description here enter image description here enter image description here

Upvotes: 0

Views: 237

Answers (1)

Peter Haddad
Peter Haddad

Reputation: 80952

Create a FutureBuilder widget, and assign the method getdata() to the future property.

Future getdata() async{
              List<Profile> list=[];
              list = await Firestore.instance
              .collection("users").getDocuments().then((querySnapshot){
               querySnapshot.documents.forEach((document) {
               list.add( Profile(
                    photos: [document['photoUrl'],],
                    name: document['nickname'],
                    age: 2,
                    distance: 2,
                    education: document['photoUrl']

                ));

            });
            return list;
        });
}

         FutureBuilder(
            future: getdata(),
            builder: (context, AsyncSnapshot<QuerySnapshot> snapshot) {
              if (snapshot.connectionState == ConnectionState.done) {
              //...

Upvotes: 1

Related Questions