Language: Map Literal is interpreted as Set Literal

Created on 15 May 2019  Â·  2Comments  Â·  Source: dart-lang/language

const Map currencies = >{'EUR':2};

A value of type 'Set>' can't be assigned to a variable of type 'Map'.
Try changing the type of the variable, or casting the right-hand type to 'Map'.

pub version
Pub 2.3.0

Flutter (Channel master, v1.5.9-pre.194, on Microsoft Windows [Version 6.3.9600], locale en-US)
    • Flutter version 1.5.9-pre.194 at M:\flutter
    • Framework revision 3436e165c8 (6 days ago), 2019-05-09 10:37:34 -0700
    • Engine revision 644db5a49c
    • Dart version 2.3.1 (build 2.3.1-dev.0.0 a0290f823c)
question

All 2 comments

<Map<String, int>>{'EUR':2} has type Map<Map<String, int>>. I think you want erase the <Map and the final > from the type annotation. e.g. <String>[] is a List of Strings and <String, int>{} is a Map of Strings-to-ints.

The expression <Map<String, int>>{'EUR': 2} is invalid.

It is a set-or-map literal with one type argument. That makes it a set literal, completely independent of the content of the literal. A <...>{...} with exactly one type argument is always a set.
In this case, the static type is precisely Set<Map<String, int>>.

However, the literal then contains a key: value entry which is invalid in a set literal.
The compiler should have recognized the invalid set literal content. Maybe it would have at some point, but before that it recognized that you cannot assign a set to a map-typed variable and reported that compile-time error first.

So, the compiler is correct (there is no rule saying which compile-time errors it must report, just that it mustn't accept a program with any compile-time errors).

You will want <String, int>{'EUR': 2} instead, which is a map literal with static type Map<String, int>.

(This issue really belongs in the SDK repository, http://github.com/dart-lang/sdk, since it is reporting an implementation issue.)

Was this page helpful?
0 / 5 - 0 ratings

Related issues

natebosch picture natebosch  Â·  4Comments

kevmoo picture kevmoo  Â·  3Comments

lrhn picture lrhn  Â·  4Comments

panthe picture panthe  Â·  4Comments

dev-aentgs picture dev-aentgs  Â·  3Comments