类型'Future<dynamic>'不是类型'List<Profile>'的子类型。

问题描述 投票:0回答:1
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
      ),

    );
  }

}

当我运行我的代码时,它返回错误:类型'Future'不是类型'List'的子类型在类型铸造.我如何解决它? 它是matchEngine运行之前,我从firebase获得数据?我试着把 await 添加到 List demoProfiles = getdata() 作为 await List;但它返回错误。我真的是新来的flutter,这个问题一直困扰着我一整天。

enter image description hereenter image description hereenter image description here

firebase flutter future
1个回答
1
投票

创建一个 FutureBuilder 小组件,并将方法分配给 getdata()future 财产。

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) {
              //...
© www.soinside.com 2019 - 2024. All rights reserved.