简体   繁体   中英

flutter method called null

I am trying to display a badge to show my uses that they have an unread message but I keep getting this error 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

here is my code

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

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

Add null check before using chatlist, because your chatlist is null.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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