【问题标题】:type 'Future<dynamic>' is not a subtype of type 'List<Profile>'Future<dynamic>' 类型不是 'List<Profile> 类型的子类型
【发布时间】:2020-09-08 21:08:35
【问题描述】:
  class Profile {

   final List<String> photos;
   final String name;
   final int age;
   final String education;
   final String bio;
   final int distance;

   Profile({
   this.photos,
   this.name,
   this.age,
   this.education,
   this.bio,
   this.distance
    });

    }



     class _MainControllerState extends State<MainController> {


     static  List<Profile> demoProfiles =   fetchData();

     static fetchData() async{

      final db = await Firestore.instance;
      List<Profile> list = [];
      db.collection("users").getDocuments().then((querySnapshot){
         querySnapshot.documents.forEach((document) {
            list.add(Profile(
                photos: document['photoUrl'],
                name: document['photoUrl'],
                age: document['photoUrl'],
                distance: document['photoUrl'],
                education: document['photoUrl']
            ));
            });
            });
            return list;
             }


           final MatchEngine matchEngine = MatchEngine (
           matches:demoProfiles.map((Profile profile) => Match(profile:
           profile)).toList()
            );                  

我是新来的颤振。 当我运行我的代码时,我得到了错误:类型'Future'不是'List'类型的子类型。如果我改变屏幕,我会得到错误:NoSuchMethodError:方法'map'被调用为null。我该如何解决? 谢谢你帮助我。

【问题讨论】:

    标签: flutter dart google-cloud-firestore


    【解决方案1】:

    需要指定方法fetchData的返回类型

    static Future<List<Profile>> fetchData() async{
    

    【讨论】:

    • 我听从了您的建议,然后又遇到了一些新错误。请看一下我更新的截图
    • @ZivCheung 所以你试图直接在synchronous 代码中访问一个async 函数,这是行不通的。那么您可以做什么将demoProfiles 的类型更改为Future&lt;List&lt;Profile&gt;&gt;,然后在async 函数中的某处使用await demoProfiles,或者您应该在未来解决后分配返回值
    • @ZivCheung 是的,肯定很想...你能分享一些关于你正在尝试实施的更多细节吗?
    • 我更新了原始代码示例的原始代码截图,我想更新我在Firestore中的数据列表
    • @ZivCheung 您可以通过FutureBuilder 小部件生成显示这些配置文件的小部件。看看this
    【解决方案2】:

    您需要将方法转换为 getData

      Future<List<Data>> getData() async {
            var response =
            await http.get(Uri.https('jsonplaceholder.typicode.com', 'users'));
            var jsonData = jsonDecode(response.body);
            List<Data> dataList = [];
                 for (var u in jsonData) {
            Data data = Data(u["name"], u["phone"], u["email"]);
            dataList.add(data);
           }
           print(dataList.length);
          return dataList;
       }
    

    并显示在卡片中

             @override
             Widget build(BuildContext context) {
              return Scaffold(
              appBar: AppBar(
              title: Text("Data Fetch"),
               ),
             body: Container(
                   child: Card(
                         child: FutureBuilder<List<Data>>(
                           future: getData(),
                    builder: (context, snapshot) {
                    if (snapshot.data == null) {
                       return Container(
                             child: Text("Loading"),
                              );
                     }else{
                        return ListView.builder(
                               itemCount: snapshot.data!.length,
                               itemBuilder: (context, i) {
                                    return ListTile(
                                       title: Column(
                                            children: [
                                                Text(snapshot.data![i].name),
                                                Text(snapshot.data![i].phone),
                                                Text(snapshot.data![i].email),
                                                ],
                                               ),
                                              );
                                           });
                                         }
                                       },
                                     ),
                                   ),
                               ));
                             }
    

    它对我有用 :) :) 我希望这会对你有所帮助。

    【讨论】:

      猜你喜欢
      • 2020-09-10
      • 2020-09-20
      • 1970-01-01
      • 2021-08-13
      • 1970-01-01
      • 2020-02-28
      • 2021-10-20
      • 2019-12-31
      • 2022-01-23
      相关资源
      最近更新 更多