1

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

2 Answers 2

1

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;
}
Sign up to request clarification or add additional context in comments.

2 Comments

now my list doesnt get anymore and get [ERROR:flutter/lib/ui/ui_dart_state.cc(209)] Unhandled Exception: type '_TypeError' is not a subtype of type 'DioError' in type cast errors
what is your data's type?
1

You can declare it as nullable:

List<MessageModel> ?messageList = [];

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.