dart - Dart 给出未处理的异常:类型不是“值”类型的子类型
问题描述
我有一个抽象类ImageUpdate
。两个类,NewImage
并ExistingImage
实现ImageUpdate
.
我有一个imageUpdates
类型的变量List<ImageUpdate>
。
当我尝试将类型变量添加NewImage
到列表时,我收到此错误:
Unhandled Exception: type 'NewImage' is not a subtype of type 'ExistingImage' of 'value'
我很困惑,因为列表是 ofImageUpdate
而不是ExistingImage
,所以我不知道如何调试它,特别是因为 Dart 是类型安全的(我没有在任何地方使用dynamic
)。
任何帮助将不胜感激,谢谢。
解决方案
我怀疑您的代码类似于:
class Base {}
class Derived1 extends Base {}
class Derived2 extends Base {}
List<Base> makeList() {
var list = [Derived1()];
return list;
}
void main() {
var list = makeList();
list.add(Derived2()); // TypeError
}
发生的情况是该List
对象最初创建为List<Derived1>
. (也就是说,list.runtimeType
将类似于List<Derived1>
,而不是它的静态(声明的)类型List<Base>
。)然后,当您尝试将Derived2
对象添加到list
时,它将在运行时失败,因为list
的实际运行时类型是List<Derived1>
,它不允许Derived2
元素。
这最终源于 Dart 允许隐式类型转换 from GenericClass<Derived>
to GenericClass<Base>
ifDerived
派生自Base
。这在许多情况下很有用,但它也可能导致像这样的情况在运行时失败。
您可以通过明确声明您想要一个List<Base>
对象来解决此问题:
List<Base> makeList() {
var list = <Base>[Derived1()]; // Note the explicit type.
return list;
}
或者,如果这不可能,则通过创建一个新List<Base>
对象:
var list = <Base>[...makeList()];
var list = List<Base>.from(makeList());
(在这种特殊情况下,List.of(makeList())
也可以使用,因为它会创建一个List
具有静态类型的新对象,但我不会使用它,因为明确类型转换会更具可读性。)
推荐阅读
- scala - 如果对象中的某些字段相同,则对象的scala过滤器列表
- javascript - 是否所有 JavaScript Iterables 都有 next() 方法?
- ssl - Filebeat:由未知权威签署的证书
- h2 - Derby 的 H2 INIT 等效项
- r - 基于条件的滞后数据(非固定滞后)
- bash - 如何在两种模式之间读取文件Shell
- c++11 - std::map 的 std::_Rb_tree 副本导致递归导致 SEGSEV 具有 4000+ 深的核心转储调用堆栈
- powershell - Powershell - 拆分/组织属性的方法?
- docker - 为什么我的 Play Framework for Scala 应用程序的 Docker 映像没有以 AccessDeniedException 开头?
- node.js - 如何在nodejs中动态调用节点调度程序