Home > OS >  Flutter Unhandled Exception: type 'Null' is not a subtype of type 'List<dynamic>
Flutter Unhandled Exception: type 'Null' is not a subtype of type 'List<dynamic>

Time:12-08

I need to assign a model to a list in the application, but I am getting the error I mentioned in the title.

Although I get this error on the android side, I do not have a problem, but when I try it on the ios side, my application crashes.

List<MessageModel> messageList = [];
String? message;
bool success = false;

@override
MessageService decode(dynamic data) {
messageList = (data as List).map((e) => MessageModel.fromJsonData(e)).toList(); ----> Unhandled Exception: type 'Null' is not a subtype of type 'List<dynamic>' in type cast
return this;
}

CodePudding user response:

    List<MessageModel> ?messageList = [];
String? message;
bool success = false;

@override
MessageService decode(dynamic data) {
messageList = (data as List).map((e) => MessageModel.fromJsonData(e)).toList(); ----> Unhandled Exception: type 'Null' is not a subtype of type 'List<dynamic>' in type cast
return this;
}

CodePudding user response:

The reason you get the error is data is null.

Try my solution:

List<MessageModel> messageList = [];
String? message;
bool success = false;

@override
MessageService decode(dynamic data) {
  messageList = data?.map((e) => MessageModel.fromJsonData(e))?.toList() ?? [];
  return this;
}
  • Related