Jonathan
Jonathan

Reputation: 164

How can I fix _TypeError in Flutter?

I have a trivial map like this:

  Map _datiMap = box.get("exc") ?? {};

when it runs:

_datiMap.addAll({"${DateFormat("y-MM-dd", "${AppLocalizations.of(context)!.codice}").format(_giornoSelezionato)}" : "${_oreProgrammateGiornoAttuale! + 1}"});

throws this error:

type '_Map<dynamic, dynamic>' is not a subtype of type 'Map<String, dynamic>' of 'other'

Upvotes: -1

Views: 109

Answers (2)

jbryanh
jbryanh

Reputation: 2023

I think this is related to "improvements" in Dart's type handling.

I agree, with Map<dynamic, dynamic> being passed to a Map<String, dynamic>, as long as the key is a String, why error? However, the decision was made to enforce explicit type declaration and not infer.

So, you'll need to write Map<String, dynamic>.from(value) to pass value to a Map<String, dynamic>.

To specifically answer your question, I presume your Map was inferred to be a Map<String, dynamic>, so you need to write:

_datiMap.addAll(Map<String, dynamic>.from({"${DateFormat("y-MM-dd","${AppLocalizations.of(context)!.codice}").format(_giornoSelezionato)}" : "${_oreProgrammateGiornoAttuale! + 1}"}));

Hive boxes are inherently Map<String, dynamic> I think.

Upvotes: 0

krishnaacharyaa
krishnaacharyaa

Reputation: 25070

Try to explicity state the Map type like

Map<String, dynamic> _datiMap = box.get("exc") ?? {};

Upvotes: 0

Related Questions