繁体   English   中英

如何修复未处理的异常:NoSuchMethodError:在 null flutter firestore 上调用了方法“[]”

[英]How to fix Unhandled Exception: NoSuchMethodError: The method '[]' was called on null flutter firestore

我正在尝试实现删除评论的方法(如果它是我自己的评论,或者在我的帖子下发布的评论),报告用户和/或阻止用户(如果我发现他们的评论令人反感或其他)。 但是,我调用对话框菜单的方法:

CommentModel com;

handleComments(BuildContext context) async {
DocumentSnapshot snap = await 
commentRef.doc(widget.post.postId).collection('comments')
.doc().get();
com = CommentModel.fromJson(snap.data());
var commentUserId = com.userId;
bool isMe = widget.post.userId == commentUserId;
bool isComUser = currentUserId() == commentUserId;
if (isMe || isComUser)
  showDialog(
    context: context,
    builder: (context) {
      return SimpleDialog(
        backgroundColor: Colors.red,
        shape: RoundedRectangleBorder(
            borderRadius: BorderRadius.circular(15.0)),
        children: [
          SimpleDialogOption(
            onPressed: () {
              deleteComment(context);
            },
            child: Text(Languages
                .of(context)
                .deleteComment, style: TextStyle(color: Colors.white),),
          ),
        ],
      );
    });
else
  showDialog(
      context: context,
      builder: (context) {
        return SimpleDialog(
            backgroundColor: Colors.red,
            shape: RoundedRectangleBorder(
                borderRadius: BorderRadius.circular(15.0)),
            children: [
SimpleDialogOption(
onPressed: () {
blockUser(context);
},
child: Text(Languages
    .of(context)
    .blockUser, style: TextStyle(color: Colors.white),),
),
Divider(),
SimpleDialogOption(
onPressed: () {
reportUser();
},
child: Text(Languages
    .of(context)
.reportUser, style: TextStyle(color: Colors.white),),
),
        ]);
      });

}

给出错误:

Unhandled Exception: NoSuchMethodError: The method '[]' was called on null.
E/flutter (13165): Receiver: null
E/flutter (13165): Tried calling: []("username")
E/flutter (13165): #0      Object.noSuchMethod (dart:core- 
patch/object_patch.dart:63:5)
E/flutter (13165): #1      new CommentModel.fromJson 
(package:blahblah/comments.dart:17:20)
E/flutter (13165): #2      _CommentsState.handleComments 
(package:blahblah/comment.dart:438:19)
E/flutter (13165): <asynchronous suspension>`

即使向 firestore 添加评论没有问题,并且 CommentModel 具有文档中的所有字段(请参见图片) 火店

问题可能出在哪里? 我浏览了几个类似的帖子,但找不到解决方案。 感谢您的帮助!

这是 CommentModel 类:

class CommentModel {
String username, comment, userDp, userId, docId;
Timestamp timestamp;

CommentModel({
this.username,
this.comment,
this.timestamp,
this.userDp,
this.userId,
this.docId
});

CommentModel.fromJson(Map<String, dynamic> json) {
username = json['username'];
comment = json['comment'];
timestamp = json['timestamp'];
userDp = json['userDp'];
userId = json['userId'];
docId = json['docId'];
}

Map<String, dynamic> toJson() {
final Map<String, dynamic> data = new Map<String, dynamic>();
data['username'] = this.username;
data['comment'] = this.comment;
data['timestamp'] = this.timestamp;
data['userDp'] = this.userDp;
data['userId'] = this.userId;
data['docId'] = this.docId;
return data;
}

}

来自两个相关线程,此问题源于 Firestore 无法找到您请求的文档。 我查看了您所附的图片,它在comments集中显示了多个文档。 您的请求中的第二个doc()调用似乎缺少所需的documentID 参数

DocumentSnapshot snap = await commentRef.doc(widget.post.postId).collection('comments').doc().get()

我在测试 Flutter 应用程序中测试了一个类似的查询,在我的情况下它也返回了null 在所有doc()方法调用上提供 documentID 后,我的问题得到解决。 如果您想从comments集合中获取所有文档,您可以使用 FlutterFire 的此文档页面

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM