颤振方法称为 null

时间:2021-04-12 09:28:09

标签: firebase flutter

我正在尝试显示徽章以显示我的用户有未读消息,但我一直收到此错误 The method '[]' was called on null. Receiver: null Tried calling: []("recieverId") The relevant error-causing widget

这是我的代码

   class ViewLayout extends StatelessWidget {
  final User contact;
  final ChatMethods _chatMethods = ChatMethods();

  ViewLayout({
    @required this.contact,
  });

  bool isread;
  Map<dynamic, dynamic> chatlist;
  @override
  Widget build(BuildContext context) {
    final UserProvider userProvider = Provider.of<UserProvider>(context);
    FirebaseUser user;
    if (chatlist['recieverId'] == user.uid) {
      isread = true;
    } else {
      isread = chatlist['isread'] == null ? true : chatlist['isread'];
    }

培训班

    class Convo {
  Convo({this.chatlist});

  factory Convo.fromFireStore(DocumentSnapshot doc) {
    final Map<String, dynamic> data = doc.data;

    return Convo(chatlist: data['chatlist'] ?? <dynamic>{});
  }

  Map<dynamic, dynamic> chatlist;
}

1 个答案:

答案 0 :(得分:0)

在使用聊天列表之前添加空检查,因为您的聊天列表为空。

if(chatlist !=null){
    if (chatlist['recieverId'] == user.uid) {
          isread = true;
        } else {
          isread = chatlist['isread'] == null ? true : chatlist['isread'];
        }
}