0

我的 futurebuilder 发生错误高于任何人有任何解决方案相关的导致错误的小部件是:FutureBuilder .................... ..................................................... ..................................................... ..................................................... ..................................................... ...................................................... ............

noti.dart
          Widget build(BuildContext context) {
            return FutureBuilder(
                future: postReference
                    .document(userId)
                    .collection("usersPosts")
                    .document(postId)
                    .get(),
                builder: (context, datasnapshot) {
                  if (!datasnapshot.hasData) {
                    return circularProgress();
                  }
                  Post post = Post.fromDocument(datasnapshot.data);
                  return Center(
                    child: Scaffold(
                      appBar: header(context, strTitle: post.description),
                      body: ListView(
                        children: <Widget>[
                          Container(
                            child: post,
                          ),
                        ],
                      ),
                    ),
                  );
                });


    Post.dart

class Post extends StatefulWidget {
  final String postId;
  final String ownerId;
  // final String timestamp;
  final dynamic likes;
  final String username;
  final String description;
  final String location;
  final String url;
  //
  Post({
    this.postId,
    this.ownerId,
    // this.timestamp,
    this.likes,
    this.username,
    this.description,
    this.location,
    this.url,
  });

  factory Post.fromDocument(DocumentSnapshot documentSnapshot) {
    return Post(
      postId: documentSnapshot["postId"],
      ownerId: documentSnapshot["ownerId"],
      likes: documentSnapshot["likes"],
      // timestamp: documentSnapshot["timestamp"],
      username: documentSnapshot["username"],
      description: documentSnapshot["description"],
      location: documentSnapshot["location"],
      url: documentSnapshot["url"],
    );
  }

引用它的 post 方法:

displayPost(context) {
  Navigator.push(
    context,
    MaterialPageRoute(
      builder: (context) => PostScreen(
        postId: postId,
        userId: userId,
      ),
    ),
  );
}
4

1 回答 1

0

这可能是因为您从查询中收到空值。您想将 documentSnapshot["postId"] 映射到您的 Post 并且此事件因 null 而失败。

对于干净的代码,我建议您使用quicktype创建一个新的 Post 模型 并使用 json 映射您的 Post。然后检查 value == null:

factory Score.fromJson(Map<String, dynamic> json) => Score(
    score: json["score"] == null ? null : json["score"].toDouble(),
于 2020-10-04T12:30:39.330 回答