invalid_ factory_ method_ impl
Details about the 'invalid_factory_method_impl' diagnostic produced by the Dart analyzer.
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:
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:
import 'package:meta/meta.dart';
class Factory {
@factory
C createC() => C();
}
class C {}
Unless stated otherwise, the documentation on this site reflects Dart 3.10.3. Report an issue.