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:
dart
class C {
C() {
return this;
}
}
Common fixes
#If the constructor should create a new instance, then remove either the
return
statement or the expression:
dart
class C {
C();
}
If the constructor shouldn't create a new instance, then convert it to be a factory constructor:
dart
class C {
factory C() {
return _instance;
}
static C _instance = C._();
C._();
}
除非另有说明,文档之所提及适用于 Dart 3.7.3 版本,本页面最后更新时间: 2025-05-08。 查看文档源码 或者 报告页面问题。