【问题标题】:Flutter Firebase A value of type 'List<Future<CustomClass>>' can't be assigned to a variable of type 'List< CustomClass >?'Flutter Firebase“List<Future<CustomClass>>”类型的值不能分配给“List<CustomClass>?”类型的变量
【发布时间】:2021-10-08 07:22:24
【问题描述】:

我的 FutureBuilder 出现问题。它应该将自定义类“GeoPost”映射到来自 firebase 查询的“列表”,但在我的代码中的某处它没有将 Geopost 列表读取为非 Future

这是发生错误的代码:

FutureBuilder(
            future: postsRef.get().then((doc) {
              geoPosts = doc.docs.map((doc) async => await GeoPost.fromDoc(doc)).toList();
            }),
            builder: (context, snapshot) { }

自定义类和函数:

    class GeoPost {
      final String caption;
      final String postId;
      final String authorId;
      final int likesCount;
      final int commentCount;
      final Position position;
      final Timestamp timeStamp;
      final String? imageUrl;
      final Userdata authordata;
    
      GeoPost(this.caption, this.postId, this.authorId, this.likesCount, this.commentCount, this.position, this.timeStamp, this.imageUrl, this.authordata);
    
      static Future<GeoPost> fromDoc(DocumentSnapshot doc) async {
        return GeoPost.fromDocument(doc, await getPostUserDataFromFirestore(doc['userId']));
    
      }
    
      factory GeoPost.fromDocument(DocumentSnapshot doc, Userdata author) {
        return GeoPost(
            doc['caption'],
            doc['postId'],
            doc['userId'],
            doc['likeCount'] ?? 0,
            doc['commentCount'] ?? 0,
            doc['position'],
            doc['timeStamp'],
            doc['imageUrl'],
            author
        );
      }

Future<void> getPostUserDataFromFirestore (String did) async {
    return Userdata.fromDoc(await usersRef.doc(uid).get());
  }
    }

错误:

Error: A value of type 'List<Future<GeoPost>>' can't be assigned to a variable of type 'List<GeoPost>?'.

【问题讨论】:

    标签: firebase flutter flutter-futurebuilder


    【解决方案1】:
    • 您需要等待List&lt;Future&lt;GeoPost&gt; 中的所有Futures(将它们转换为List&lt;GeoPost&gt;),然后再将它们分配给List&lt;GeoPost&gt; 类型的变量。

      将您的 FutureBuilder 代码更新为:

      FutureBuilder<List<GeoPost>>(
        future: () async { 
          var doc = await FirebaseFirestore.instance.collection('collectionPath').get();
          var data = doc.docs.map((doc) async => await GeoPost.fromDoc(doc)).toList();
          geoPosts = await Future.wait(data);
          return geoPosts;
        }(),
        builder: (context, snapshot) { }
      
    • 另外,您需要将getPostUserDataFromFirestore 方法的返回类型从Future&lt;void&gt; 更改为Future&lt;Userdata&gt;。没有返回值时使用void

      getPostUserDataFromFirestore 方法更新为:

      Future<Userdata> getPostUserDataFromFirestore (String did) async {
        return Userdata.fromDoc(await usersRef.doc(uid).get());
      }
      

    【讨论】:

    • 它现在似乎不起作用我刚刚收到此错误:“参数类型'Future?> Function()' 无法分配给参数输入“未来?'。”
    • 不要忘记在未来函数的末尾添加两个括号,即“()”。括号返回结果而不是传递函数。
    • 我将函数放在了小部件树之外,现在它似乎工作正常,但很高兴知道这一点。非常感谢
    猜你喜欢
    • 2020-10-24
    • 2021-08-29
    • 2020-06-01
    • 1970-01-01
    • 1970-01-01
    • 2021-12-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多