首页 > 解决方案 > 转换子类型失败

问题描述

class A {
  void hello() {
    print('world');
  }
}

class B extends A {
  @override
  void hello() {
    print('StackOverflow');
  }
}

(A() as B).hello();结果type 'A' is not a subtype of type 'B' in type cast

标签: dart

解决方案


强制转换的工作方式是您只能从更具体的类型(在这种情况下为 B)转换为更通用的类型 A。您创建了 A 的实例,但 A 不是 B。


推荐阅读