non_sync_factory
Factory bodies can't use 'async', 'async*', or 'sync*'.
Description
#The analyzer produces this diagnostic when the body of a factory
constructor is marked with async
, async*
, or sync*
. All constructors,
including factory constructors, are required to return an instance of the
class in which they're declared, not a Future
, Stream
, or Iterator
.
Example
#The following code produces this diagnostic because the body of the factory
constructor is marked with async
:
class C {
factory C() async {
return C._();
}
C._();
}
Common fixes
#If the member must be declared as a factory constructor, then remove the keyword appearing before the body:
class C {
factory C() {
return C._();
}
C._();
}
If the member must return something other than an instance of the enclosing class, then make the member a static method:
class C {
static Future<C> m() async {
return C._();
}
C._();
}
除非另有说明,文档之所提及适用于 Dart 3.7.3 版本,本页面最后更新时间: 2025-05-08。 查看文档源码 或者 报告页面问题。