invalid_ factory_ method_ decl
Factory method '{0}' must have a return type.
Description
#
The analyzer produces this diagnostic when a method that is annotated with
the
factory
annotation has a return type of
void
.
Example
#
The following code produces this diagnostic because the method
createC
is annotated with the
factory
annotation but doesn't
return any value:
import 'package:meta/meta.dart';
class Factory {
@factory
void createC() {}
}
class C {}
Common fixes
#Change the return type to something other than void
:
import 'package:meta/meta.dart';
class Factory {
@factory
C createC() => C();
}
class C {}
Unless stated otherwise, the documentation on this site reflects Dart 3.9.2. Page last updated on 2025-9-1. View source or report an issue.