How to enable 'constructor-tearoffs' feature?

Question

Asked by Federico C on November 09, 2021 (source).

When I try to get the type of a class with generic

class Dummy<T> {
  T? value;
}

void main() {
  Type t = Dummy<int>;
}

I get this error:

This requires the 'constructor-tearoffs' language feature to be enabled.
Try updating your pubspec.yaml to set the minimum SDK constraint to 2.14.0 or higher, and running 'pub get'.

My pubspec.yaml containes the right SDK version, and I run already flutter clean and flutter pub get

environment:
  sdk: ">=2.14.4 <3.0.0"

How do I solve it?

Answer

Question answered by mmcdon20 (source).

Constructor tearoffs are not going to help you accomplish this. The only approach I'm aware of that could work here is to use a typedef assigned to Dummy<Int> and assign t to that.

class Dummy<T> {
  T? value;
}

typedef DummyInt = Dummy<int>;

void main() {
  Type t = DummyInt;
}

For anyone who actually does want to enable constructor tearoffs see the documentation on experimental flags. Use the flag constructor-tearoffs either by passing it in as a command line argument like dart --enable-experiment=constructor-tearoffs bin/your_program.dart or by configuring it in your analysis_options.yaml file or Visual Studio Code settings as described in the link.

DART FLUTTER GENERICS TYPES
SHARE: