我可以向下转换一个类,使它丢失原始的构造函数吗?

问题描述


class Foo {
  const Foo(this.message);
  final String message;
}

class Bar extends Foo {
  const Bar(String message,this.another,) : super(message); 
  final String another;
}

void main() {
  const Foo _foo = Bar('hello','world');
  const Bar _bar = _foo;
  print(_bar.another);
}

  • 在撰写本文时,使用darpad Based on Flutter 1.22.0-12.1.pre Dart SDK 2.9.3

    输出world


这是我们每Widget遇到的典型行为,

但是有一种方法可以强制Object丢失“额外的”构造函数

是否要重铸整个对象

解决方法

如果没有人打电话,它会因树木摇摇欲坠而消失。