Dart/Flutter linter 规则:索引地图的类型应该是地图的键类型?
Dart/Flutter linter rule: the type to index a map should be the key type of map?
比如我有Map<int, int> m;
。然后我可以写下 m['hello']
而不会出现任何编译时错误,但是当然,在运行时找不到任何元素。我希望它会在编译时或 lint 时产生错误(或警告)。
在很多情况下这是个大问题。例如,当我将 Map<A, int> m
重构为 Map<B, int> m
时,我希望像 m[some_var_of_type_A]
这样的所有访问都有编译时错误,而不是没有编译时错误,然后突然在运行时爆炸。再举个例子,反序列化的 JSON 是 Map<String, ...>
类型,但 key 实际上是一个 int。所以很想做 var userId=42; deserializedJson[userId]
但只是发现错误。实际上需要做deserializedJson[userId.toString()]
.
你知道,dart 的类型系统非常强大(甚至是 null 安全!),我真的很喜欢它,因为它在编译时发现了很多错误。所以我希望这个问题也能在编译时得到解决。
感谢任何建议!
注意:
原答案有误,已编辑为:
- 指出right/better答案
- 解释原答案错误的原因
感谢@jamesdlin 指出这一点。
更好的答案
正如@jamesdlin 在他的 中指出的那样,问题中提到的 lint 规则已在 flutter Github 问题中被请求,但尚未投入生产。
原答案(错误但与问题有点相关)
错误原因:
问题是询问使用Map索引时的lint规则。然而,答案给出了关于使用错误索引初始化地图的 lint 规则(通过错误的索引,我的意思是不同的数据类型)。
答案如下:
对此有一个 lint 规则。
例如,如果你这样定义一个 Map ->
final Map<String, String> m = {
1: 'some random value',
};
它会立即显示错误并且无法编译。这是错误 ->
Error: A value of type 'int' can't be assigned to a variable of type 'String'.
1: 'error because index is of type String but assigned value is of type int',
^
Error: Compilation failed.
请参阅定义此 lint 规则 map_key_type_not_assignable 的官方文档。
我已经在 dartpad 和 vs 代码中对此进行了测试。 IDE 都显示此错误。
如果您没有看到此 lint 错误,则您的 IDE 配置可能存在一些问题。
至于你的问题,如上所述,已经有一个 lint 规则。
目前没有 lint 来警告在 Map
上使用错误类型的参数进行查找。这已在 https://github.com/dart-lang/linter/issues/1307.
中提出要求
另见 https://github.com/dart-lang/sdk/issues/37392, which requests a type-checked alternative to Map.operator []
. In the meantime, Dart's extension mechanism allows anyone to easily add such an alternative themselves. For example, package:basics
provides a type-checked Map.get
扩展。
比如我有Map<int, int> m;
。然后我可以写下 m['hello']
而不会出现任何编译时错误,但是当然,在运行时找不到任何元素。我希望它会在编译时或 lint 时产生错误(或警告)。
在很多情况下这是个大问题。例如,当我将 Map<A, int> m
重构为 Map<B, int> m
时,我希望像 m[some_var_of_type_A]
这样的所有访问都有编译时错误,而不是没有编译时错误,然后突然在运行时爆炸。再举个例子,反序列化的 JSON 是 Map<String, ...>
类型,但 key 实际上是一个 int。所以很想做 var userId=42; deserializedJson[userId]
但只是发现错误。实际上需要做deserializedJson[userId.toString()]
.
你知道,dart 的类型系统非常强大(甚至是 null 安全!),我真的很喜欢它,因为它在编译时发现了很多错误。所以我希望这个问题也能在编译时得到解决。
感谢任何建议!
注意:
原答案有误,已编辑为:
- 指出right/better答案
- 解释原答案错误的原因
感谢@jamesdlin 指出这一点。
更好的答案
正如@jamesdlin 在他的
原答案(错误但与问题有点相关)
错误原因:
问题是询问使用Map索引时的lint规则。然而,答案给出了关于使用错误索引初始化地图的 lint 规则(通过错误的索引,我的意思是不同的数据类型)。
答案如下:
对此有一个 lint 规则。
例如,如果你这样定义一个 Map ->
final Map<String, String> m = {
1: 'some random value',
};
它会立即显示错误并且无法编译。这是错误 ->
Error: A value of type 'int' can't be assigned to a variable of type 'String'.
1: 'error because index is of type String but assigned value is of type int',
^
Error: Compilation failed.
请参阅定义此 lint 规则 map_key_type_not_assignable 的官方文档。
我已经在 dartpad 和 vs 代码中对此进行了测试。 IDE 都显示此错误。
如果您没有看到此 lint 错误,则您的 IDE 配置可能存在一些问题。
至于你的问题,如上所述,已经有一个 lint 规则。
目前没有 lint 来警告在 Map
上使用错误类型的参数进行查找。这已在 https://github.com/dart-lang/linter/issues/1307.
另见 https://github.com/dart-lang/sdk/issues/37392, which requests a type-checked alternative to Map.operator []
. In the meantime, Dart's extension mechanism allows anyone to easily add such an alternative themselves. For example, package:basics
provides a type-checked Map.get
扩展。