no_ logic_ in_ create_ state
Learn about the no_logic_in_create_state linter rule.
Don't put any logic in createState.
Details
#
DON'T put any logic in
createState().
Implementations of createState() should return a
new instance of a State object and do nothing more. Since
state access is preferred via the widget field,
passing data to State objects using custom
constructor parameters should also be avoided and so further,
the State constructor is required to be passed no arguments.
BAD:
MyState global;
class MyStateful extends StatefulWidget {
@override
MyState createState() {
global = MyState();
return global;
}
}
class MyStateful extends StatefulWidget {
@override
MyState createState() => MyState()..field = 42;
}
class MyStateful extends StatefulWidget {
@override
MyState createState() => MyState(42);
}
GOOD:
class MyStateful extends StatefulWidget {
@override
MyState createState() {
return MyState();
}
}
Enable
#
To enable the no_logic_in_create_state rule, add
no_logic_in_create_state under
linter > rules in your
analysis_options.yaml
file:
linter:
rules:
- no_logic_in_create_state
If you're instead using the YAML map syntax to configure
linter rules, add
no_logic_in_create_state: true under
linter > rules:
linter:
rules:
no_logic_in_create_state: true
除非另有说明,文档之所提及适用于 Dart 3.12.2 版本报告页面问题.