redirect_to_invalid_function_type
The redirected constructor '{0}' has incompatible parameters with '{1}'.
Description
#The analyzer produces this diagnostic when a factory constructor attempts to redirect to another constructor, but the two have incompatible parameters. The parameters are compatible if all of the parameters of the redirecting constructor can be passed to the other constructor and if the other constructor doesn't require any parameters that aren't declared by the redirecting constructor.
Examples
#The following code produces this diagnostic because the constructor for A
doesn't declare a parameter that the constructor for B
requires:
abstract class A {
factory A() = B;
}
class B implements A {
B(int x);
B.zero();
}
The following code produces this diagnostic because the constructor for A
declares a named parameter (y
) that the constructor for B
doesn't
allow:
abstract class A {
factory A(int x, {int y}) = B;
}
class B implements A {
B(int x);
}
Common fixes
#If there's a different constructor that is compatible with the redirecting constructor, then redirect to that constructor:
abstract class A {
factory A() = B.zero;
}
class B implements A {
B(int x);
B.zero();
}
Otherwise, update the redirecting constructor to be compatible:
abstract class A {
factory A(int x) = B;
}
class B implements A {
B(int x);
}
除非另有说明,文档之所提及适用于 Dart 3.7.3 版本,本页面最后更新时间: 2025-05-08。 查看文档源码 或者 报告页面问题。