【问题标题】:The method '[]' was called on null. Flutter?在 null 上调用了方法“[]”。扑?
【发布时间】:2021-01-19 10:00:06
【问题描述】:

我的 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,
      ),
    ),
  );
}

【问题讨论】:

  • 添加 Post 类

标签: android flutter flutter-futurebuilder


【解决方案1】:

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

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

factory Score.fromJson(Map<String, dynamic> json) => Score(
    score: json["score"] == null ? null : json["score"].toDouble(),

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2020-11-15
    • 2021-11-15
    • 2021-09-22
    • 2021-06-11
    • 2021-03-06
    • 2021-07-13
    • 2020-01-17
    • 2020-02-21
    相关资源
    最近更新 更多