在dart中的地图列表中没有类型检查? [英] Is there no type checking in a list of maps in dart?

查看:130
本文介绍了在dart中的地图列表中没有类型检查?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

例如,如果您有以下列表:

For example if you have the list:

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

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

不应该有string in place of int的警告吗?

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

推荐答案

在Dart中,静态检查器不会对每一种可能的类型违例提出抱怨,因为代码是正确的。

In Dart the static checker does not complain about every possible type violation because there is a chance the code is correct. At runtime when an illegal operation is done you get an exception.

当我使用时:

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

我创建一个地图:地图< dynamic,dynamic> ,动态关闭静态检查器。

I make a map: Map<dynamic,dynamic>, dynamic shuts up the static checker.

Dart允许这个代码运行,因为下划线可能是有效的,Dart乐观地知道你在做什么。

Dart allows this code to run because down-assignments may be valid and Dart is optimistic that you know what you’re doing.

所以我的Map可以是 Map< String,int> 那么它是正确的,如果没有抛出异常。

So my Map could be Map<String,int> then it's correct, if not an exception is thrown.

这篇关于在dart中的地图列表中没有类型检查?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆