0

I'm struggling to see how these type errors could be resolved in Flutter.

I am making an HTTP request to a server which responds with the following JSON:

{
  "message": "The given data was invalid.",
  "errors": {
    "email": ["Email is required"]
  }
}

I am assigning the data from the response like so:

Future login(String email, String password) async {
    ...
    Map<String, dynamic> data = json.decode(response.body);

    final List<String> emailErrors = data['errors']['email'];
    final List<String> passwordErrors = data['errors']['password'];
}

Once I try to do that I get the following error:

Unhandled Exception: type 'List' is not a subtype of type 'List'

It seems like using type dynamic it will pretty much throw an error if you try to set a type annotation afterwards for any type. How can this be solved?

1 Answer 1

1

You get this error because the data['errors']['email']returns a List<dynamic>.

You have to cast items to String like that :

final List<String> emailErrors = data['errors']['email'].cast<String>()

Sign up to request clarification or add additional context in comments.

2 Comments

Every element, even the nested ones will be inferred as the dynamic type because that’s what I annotated in my map?
Exactly, all elements

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.