How do I cast InternalLinkedHashMap as Map<dynamic, dynamic>?

6,358

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));
Share:
6,358
Joel Broström
Author by

Joel Broström

Updated on December 13, 2022

Comments

  • Joel Broström
    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 that message is InternalLinkedHashMap and cannot be cast too Map<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
    Joel Broström over 4 years
    I get why String might be a problem, but why is Map<dynamic, dynamic> an issue? Shouldn't any HashMap be able to be converted to Map<dynamic, dynamic>?
  • MarcG
    MarcG over 4 years
    But you are doing the opposite. You are not converting "to", but "from" Map<dynamic, dynamic>. You are taking a map where keys are dynamic, and you are trying to put its content into a map that only accepts String keys. Got it?
  • Joel Broström
    Joel Broström over 4 years
    In 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
    MarcG over 4 years
    The 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
    Joel Broström over 4 years
    Right, 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
    Sandeep Singh over 3 years
    Thanks for the correct answer, I wasted hrs. to get this answer. Can you please also suggest how to get list
  • Joel Broström
    Joel Broström over 2 years
    To 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);