Search code examples
flutterdarttype-inferencedowncastinference

Can I down-cast a class forcing it to lose the original constructor/s?



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);
}

  • at the moment of writing, with darpad Based on Flutter 1.22.0-12.1.pre Dart SDK 2.9.3

    the output is world


this is the typical behavior that we experience with every Widget,

but is there a way to force the Object to lose the "extra" constructor

without recasting the whole Object?


Solution

  • If nobody calls it, it will go away due to treeshaking.