Carlos Garmon
Carlos Garmon

Reputation: 11

How can I initialize an empty LinkedHashMap on dart?

When I try to initialize the LinkedHashMap as _map = {}; this happens:

A value of type 'Map<dynamic, dynamic>' can't be assigned to a variable of type LinkedHashMap<DateTime, List<Event>>.

I tried casting it as: _map = {} as LinkedHashMap<DateTime List<Event>>; but I get:

type '_InternalLinkedHashMap<dynamic, dynamic>' is not a subtype of type 'LinkedHashMap<DateTime, List>' in type cast.

I don't have a lot of experience so I'm not sure if I'm doing something kinda dumb.

Upvotes: 1

Views: 1584

Answers (1)

jamesdlin
jamesdlin

Reputation: 90155

You can just call the LinkedHashMap constructor directly:

_map = LinkedHashMap<DateTime, List<Event>>();

Note that doing so might trigger the prefer_collection_literals lint (see https://github.com/dart-lang/linter/issues/1649), but in my opinion it's fine to ignore that if you want to be explicit.

Alternatively, if you don't mind being implicit, just rely on the default Map implementation already being a LinkedHashMap:

_map = <DateTime, List<Event>>{};

Upvotes: 1

Related Questions