invalid_factory_name_not_a_class
The name of a factory constructor must be the same as the name of the immediately enclosing class.
Description
#The analyzer produces this diagnostic when the name of a factory constructor isn't the same as the name of the surrounding class.
Example
#The following code produces this diagnostic because the name of the factory
constructor (A
) isn't the same as the surrounding class (C
):
class A {}
class C {
factory A() => throw 0;
}
Common fixes
#If the factory returns an instance of the surrounding class, and you intend it to be an unnamed factory constructor, then rename the factory:
class A {}
class C {
factory C() => throw 0;
}
If the factory returns an instance of the surrounding class, and you intend it to be a named factory constructor, then prefix the name of the factory constructor with the name of the surrounding class:
class A {}
class C {
factory C.a() => throw 0;
}
If the factory returns an instance of a different class, then move the factory to that class:
class A {
factory A() => throw 0;
}
class C {}
If the factory returns an instance of a different class, but you can't modify that class or don't want to move the factory, then convert it to be a static method:
class A {}
class C {
static A a() => throw 0;
}
除非另有说明,文档之所提及适用于 Dart 3.7.3 版本,本页面最后更新时间: 2025-05-08。 查看文档源码 或者 报告页面问题。