Error inferring type in the abstract class constructor

I stumbled upon this case when working with abstract classes:

abstract class Test {
  final String value;

  Test(this.value); // [1]
}

abstract class Test2 {
  final String value;

  Test2(String i) : value = i; // [2]
}

class Store {}

class AppController = Test with Store;
class AppController2 = Test2 with Store;

void main() {
  var test1 = AppController(inject()); // prints 'dynamic'
  var test2 = AppController2(inject()); // prints 'String'
}

T inject<T>() {
  print(T);
}

The only difference between those two classes are the constructor signatures. The first one, Test, uses the this. shorthand; the second one, Test2, does not. I expected the behaviour to be the same, but Dart is actually inferring the wrong runtime type. I know that if I write inject<String>() it will work; I would like to understand why it doesn’t work without it, even though Test.value is of type String.

View in DartPad

Author: Fantashit

2 thoughts on “Error inferring type in the abstract class constructor

  1. Changing [1] to Test(String this.value); makes a difference too. Seems like a bug, since the parameter types should be inferred as the type of the field they’re initializing.

  2. @ds84182 I think you’re right. I work with the flutter_modular that needs dependency injection and I always have to type it that way in abstract constructors (in the case of MobX).

    Congratulations to @savioserra for identifying this error.

Comments are closed.