我很困惑为什么以下Dart代码会生成运行时错误:
class Store<ID, T> {
Store({Map<ID, T> values = const {}}) : _cache = values;
final Map<ID, T> _cache;
void put(ID id, T value) {
_cache[id] = value;
}
}
...
final foo = Store<int, Store<int, String>>();
// Runtime error inside `Store.put`:
// TypeError: 7: type 'int' is not a subtype of type 'Never'
foo.put(7, Store<int, String>());
如果我直接像这样使用Map
,这不是问题:
final foo = Map<int, Map<int, String>>();
foo[7] = Map<int, String>());
要让Store
个发挥作用,我需要了解有关Dart仿制药的某些内容吗?