How do I cast InternalLinkedHashMap as Map<dynamic, dynamic>?
Solution 1
I found the best way to solve it were to use Map.from() and specify the variable type as Map:
final Map<String, dynamic> data = Map.from(message['data']);
Solution 2
Try this:
final Map<String,dynamic> data = Map.castFrom<dynamic, dynamic, String, dynamic>(message['data']);
You should only do this, of course, if all of your keys are really Strings.
If you want to understand what's going on, see this:
var x = {};
print(x.runtimeType);
Map<String, dynamic> y = Map.castFrom<dynamic, dynamic, String, dynamic>(x);
print(y.runtimeType);
Solution 3
You can try to map all keys to string. Something like:
final Map<String,dynamic> data = message.map((key, value) => MapEntry(key.toString(), value));
Joel Broström
Updated on December 13, 2022Comments
-
Joel Broström over 1 year
Im setting up firebase cloud messaging and in my
onResume()
callback I have the following:Future<void> onResume(Map<String, dynamic> message) async { final Map<String,dynamic> data = message['data']; final String url = data['url']; if (url != null) { _webViewController?.loadUrl(url); } }
When the function reaches
final Map<String,dynamic> data = message['data'];
it returns prematurely and silently without warnings.If I instead run
final dynamic data = message['data'];
it continues as expected.Inspecting the
message
type revels thatmessage
isInternalLinkedHashMap
and cannot be cast tooMap<String, dynamic>
.It says
_InternalLinkedHashMap<dynamic, dynamic>' is not a subtype of type 'Map<String, dynamic>'
.How do I do this?
How can I find this issue in the future if it has no trace? -
Joel Broström over 4 yearsI get why
String
might be a problem, but why isMap<dynamic, dynamic>
an issue? Shouldn't anyHashMap
be able to be converted toMap<dynamic, dynamic>
? -
MarcG over 4 yearsBut you are doing the opposite. You are not converting "to", but "from"
Map<dynamic, dynamic>
. You are taking a map where keys aredynamic
, and you are trying to put its content into a map that only acceptsString
keys. Got it? -
Joel Broström over 4 yearsIn the example above, yes. But if I change data to
Map<dynamic, dynamic>
then I'm trying to cast message (Map<String, dynamic>
) to a new variable data(Map<dynamic, dynamic>
). I feel like ´String´ could be cast to ´dynamic´? -
MarcG over 4 yearsThe opposite works. This works:
Map<String, dynamic> x = {"a": 10, "b": true}; Map<dynamic, dynamic> y = x;
If you have some code that doesn't work, show me. -
Joel Broström over 4 yearsRight, that's what I was trying to convey with my previous comment. If you take my code and replace the data type to Map<dynamic, dynamic> it still fails.
-
Sandeep Singh over 3 yearsThanks for the correct answer, I wasted hrs. to get this answer. Can you please also suggest how to get list
-
Joel Broström over 2 yearsTo cast a List<dynamic> to List<int> using the above syntax you would type
final List<dynamic> dynamicList = [];
final List<int> intList = List.from(dynamicList);