Dart Analyze logoDart Analyze/
DRT-W1528

Implicit generative constructor in subclass cannot invoke explicit unnamed factory constructor in superclassDRT-W1528

Major severityMajor
Bug Risk categoryBug Risk

The analyzer produces this diagnostic when a class has an implicit generative constructor and the superclass has an explicit unnamed factory constructor. The implicit constructor in the subclass implicitly invokes the unnamed constructor in the superclass, but generative constructors can only invoke another generative constructor, not a factory constructor.

Example

The following code produces this diagnostic because the implicit constructor in B invokes the unnamed constructor in A, but the constructor in A is a factory constructor, when a generative constructor is required:

class A {
  factory A() => throw 0;
  A.named();
}

class B extends A {}

Common fixes

If the unnamed constructor in the superclass can be a generative constructor, then change it to be a generative constructor:

class A {
  A();
  A.named();
}

class B extends A { }

If the unnamed constructor can't be a generative constructor and there are other generative constructors in the superclass, then explicitly invoke one of them:

class A {
  factory A() => throw 0;
  A.named();
}

class B extends A {
  B() : super.named();
}

If there are no generative constructors that can be used and none can be added, then implement the superclass rather than extending it:

class A {
  factory A() => throw 0;
  A.named();
}

class B implements A {}