首页 > 解决方案 > 输入'未来' 不是类型 'List 的子类型' 在类型转换中

问题描述

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
      ),

    );
  }

}

当我运行我的代码时,它返回错误:type 'Future' is not a subtype of type 'List' in type cast。我该如何解决?在我从 Firebase 获取数据之前,它是 matchEngine 运行的吗?我尝试将 await 添加到 List demoProfiles = getdata() as await List; 但它返回错误。我对颤振真的很陌生,这个问题整天都在困扰着我。请帮我

在此处输入图像描述 在此处输入图像描述 在此处输入图像描述

标签: firebaseflutterdartgoogle-cloud-firestorefuture

解决方案


创建一个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) {
              //...

推荐阅读