简体   繁体   English

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

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

I am trying to implement method to delete comment (if it is my own comment, or comment that was posted under my post), report user and/or block user (if I find their comments offensive or whatever).我正在尝试实现删除评论的方法(如果它是我自己的评论,或者在我的帖子下发布的评论),报告用户和/或阻止用户(如果我发现他们的评论令人反感或其他)。 However, my method that calls dialog menu:但是,我调用对话框菜单的方法:

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

} }

gives error:给出错误:

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>`

Even though adding comments to firestore is no problem, and CommentModel has all the fields in the doc (please, see pic)即使向 firestore 添加评论没有问题,并且 CommentModel 具有文档中的所有字段(请参见图片) 火店

Where could be the issue?问题可能出在哪里? I went through several similar posts, but could not find the solution.我浏览了几个类似的帖子,但找不到解决方案。 Thank you for your help!感谢您的帮助!

here is the CommentModel class:这是 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;
}

} }

From two related threads , this issue originates from Firestore not being able to find the document you are requesting.来自两个相关线程,此问题源于 Firestore 无法找到您请求的文档。 I looked at your attached image and it shows multiple documents inside the comments subcollection.我查看了您所附的图片,它在comments集中显示了多个文档。 It appears that your second doc() call in your request is missing the required documentID argument :您的请求中的第二个doc()调用似乎缺少所需的documentID 参数

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

I tested a similar query in a test Flutter app and it also returned null in my case.我在测试 Flutter 应用程序中测试了一个类似的查询,在我的情况下它也返回了null The issue on my end was resolved after providing a documentID on all doc() method calls.在所有doc()方法调用上提供 documentID 后,我的问题得到解决。 If you would like to get all documents from the comments collection you can make use of this documentation page for FlutterFire.如果您想从comments集合中获取所有文档,您可以使用 FlutterFire 的此文档页面

暂无
暂无

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

相关问题 未处理的异常 NoSuchMethodError:方法 &#39;[]&#39; 在 Flutter 中被调用为 null - Unhandled Exception NoSuchMethodError: The method '[]' was called on null in flutter [Flutter] 未处理的异常:NoSuchMethodError:方法 &#39;[]&#39; 在 null 上被调用 - [Flutter ]Unhandled Exception: NoSuchMethodError: The method '[]' was called on null 颤振未处理的异常:NoSuchMethodError:方法&#39;then&#39;在null上被调用 - Flutter Unhandled Exception: NoSuchMethodError: The method 'then' was called on null 未处理的异常:NoSuchMethodError:在 flutter 上的 null 上调用了方法“[]” - Unhandled Exception: NoSuchMethodError: The method '[]' was called on null on flutter 未处理的异常:NoSuchMethodError:在 null 上调用了方法“[]”。 访问firestore时 - Unhandled Exception: NoSuchMethodError: The method '[]' was called on null. when accessing firestore 如何修复 [ERROR:flutter/lib/ui/ui_dart_state.cc(166)] 未处理的异常:NoSuchMethodError:在 null 上调用了方法 '&lt;='。 错误? - How to fix the [ERROR:flutter/lib/ui/ui_dart_state.cc(166)] Unhandled Exception: NoSuchMethodError: The method '<=' was called on null. error? 未处理的异常:NoSuchMethodError:在 null 上调用了方法“addAdvogado”。 关于颤振 - Unhandled Exception: NoSuchMethodError: The method 'addAdvogado' was called on null. On Flutter 未处理的异常:NoSuchMethodError:在 null 上调用了方法“关闭”。 Flutter BLoC - Unhandled Exception: NoSuchMethodError: The method 'close' was called on null. Flutter BLoC Flutter:未处理的异常:NoSuchMethodError:在 null 上调用了方法“findAncestorStateOfType” - Flutter: Unhandled Exception: NoSuchMethodError: The method 'findAncestorStateOfType' was called on null Flutter - 未处理的异常:NoSuchMethodError:在 null 上调用了方法“validate” - Flutter - Unhandled Exception: NoSuchMethodError: The method 'validate' was called on null
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM