Unhandled Exception: type ‘_Map<String, dynamic>’ is not a subtype of type ‘String?’
The other day I was working with restful api call in my flutter app and I got the above error.
We often get this error, and how to solve this error? Before solving you need to understand why you have this error at the first place.
We need to focus on two things here
- _Map<String, dynamic>
- String?
String? is the expected one, and _Map<String, dynamic> is the given data.
Simply speaking, I want you to give me String type but you are giving me Map type.
So, it’s data type mismatching between what’s being expected and what’s being given.
My server side response was giving a Map data type inside json, but my flutter app was expecting String inside the json.
You see from the picture, my data field is actually returning Map which is $orderRes
Now see my flutter code
You see on line 5, we are expecting data type String.
So to solve this error, you need to change front code or backend code. Their type has to match.
For my case, I decided to change backend code.
I changed data field to string type.
So, the data field changed the error is gone as well. I am returning empty string.
How do you handle this from the flutte app/.
Using dio https://www.dbestech.com/tutorials/flutter-dio-networking
You should manage it from Dio. Dio interceptor catch error and handles them.