簡體   English   中英

flutter 方法稱為 null

[英]flutter method called null

我正在嘗試顯示一個徽章以顯示我的用戶有未讀消息,但我不斷收到此錯誤The method '[]' was called on null. Receiver: null Tried calling: []("recieverId") The relevant error-causing widget 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

    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;
}

在使用聊天列表之前添加 null 檢查,因為您的聊天列表是 null。

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM