Is there a type check on the list of cards in the dart?

For example, if you have a list:

List<Map<String,int>> list =  <Map<String,int>>[
    {"string1": 44},
    {"string2" : "string in place of int"}
];

      

or

List<Map<String,int>> list =  new List<Map<String,int>>();
list.addAll([
    {"string1": 44},
    {"string2" : "string in place of int"}]
);

      

Shouldn't there be a warning for "string instead of int"?

+3


source to share


3 answers


To get a warning from the analyzer or DartEditor, you need to write it as



List<Map<String,int>> list =  <Map<String,int>>[
    <String,int>{"string1": 44},
    <String,int>{"string2" : "string in place of int"}
];

      

+1


source


Types are not used by Dart unless you have executed your code in validation mode.



When Dart is not in validation mode (which should take place during production) with no types at all, the wrong types or the right types make no difference (no bugs, no speed gains).

+3


source


In Dart, the static controller does not complain about all possible type violations, because there is a chance that the code is correct. At runtime, when an illegal operation is performed, you get an exception.

When I use:

{"string2" : "string in place of int"}

      

I am making a map:, Map<dynamic,dynamic>

dynamic disables static validation.

Dart lets you run this code because down assignments can be valid, and Dart is optimistic that you know what you are doing.

So my map might be Map<String,int>

, then it will be correct unless an exception is thrown.

0


source







All Articles