instance_ member_ access_ from_ factory
Details about the 'instance_member_access_from_factory' diagnostic produced by the Dart analyzer.
Instance members can't be accessed from a factory constructor.
Description
#
The analyzer produces this diagnostic when a factory
constructor contains an unqualified reference to an instance
member. In a generative constructor, the instance of the class
is created and initialized before the body of the constructor
is executed, so the instance can be bound to
this and accessed just like it would be in an
instance method. But, in a factory constructor, the instance
isn't created before executing the body, so
this can't be used to reference it.
Example
#
The following code produces this diagnostic because
x isn't in scope in the factory constructor:
class C {
int x;
factory C() {
return C._(x);
}
C._(this.x);
}
Common fixes
#Rewrite the code so that it doesn't reference the instance member:
class C {
int x;
factory C() {
return C._(0);
}
C._(this.x);
}
除非另有说明,文档之所提及适用于 Dart 3.12.2 版本报告页面问题.