当前位置: 首页 > 知识库问答 >
问题:

类型“Future”不是类型转换中“List”类型的子类型

勾向文
2023-03-14
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”类型的子类型。我该如何修复它?它是在我从Firebase获取数据之前匹配引擎运行的?我试图将wait添加到List demoProfiles=getdata()作为wait List;但它返回错误。我真的是新手,这个问题已经困扰了我一整天。请帮帮我

共有1个答案

秦才良
2023-03-14

创建一个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) {
              //...
 类似资料: