return_ in_ generative_ constructor
Constructors can't return values.
Description
#
The analyzer produces this diagnostic when a generative constructor
contains a
return
statement that specifies a value to be returned.
Generative constructors always return the object that was created, and
therefore can't return a different object.
Example
#
The following code produces this diagnostic because the
return
statement
has an expression:
class C {
C() {
return this;
}
}
Common fixes
#
If the constructor should create a new instance, then remove either the
return
statement or the expression:
class C {
C();
}
If the constructor shouldn't create a new instance, then convert it to be a factory constructor:
class C {
factory C() {
return _instance;
}
static C _instance = C._();
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.