invalid_factory_method_impl
Factory method '{0}' doesn't return a newly allocated object.
Description
#The analyzer produces this diagnostic when a method that is annotated with
the factory
annotation doesn't return a newly allocated
object.
Example
#The following code produces this diagnostic because the method createC
returns the value of a field rather than a newly created instance of C
:
dart
import 'package:meta/meta.dart';
class Factory {
C c = C();
@factory
C createC() => c;
}
class C {}
Common fixes
#Change the method to return a newly created instance of the return type:
dart
import 'package:meta/meta.dart';
class Factory {
@factory
C createC() => C();
}
class C {}
除非另有说明,文档之所提及适用于 Dart 3.7.3 版本,本页面最后更新时间: 2025-05-08。 查看文档源码 或者 报告页面问题。