目录

no_logic_in_create_state

Don't put any logic in createState.

此规则自 Dart 2.8 版本起可用。

_规则集:flutter _

详情

#

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:

dart
MyState global;

class MyStateful extends StatefulWidget {
  @override
  MyState createState() {
    global = MyState();
    return global;
  }
}
dart
class MyStateful extends StatefulWidget {
  @override
  MyState createState() => MyState()..field = 42;
}
dart
class MyStateful extends StatefulWidget {
  @override
  MyState createState() => MyState(42);
}

GOOD:

dart
class MyStateful extends StatefulWidget {
  @override
  MyState createState() {
    return MyState();
  }
}

使用方法

#

要启用 no_logic_in_create_state 规则,请在你的 analysis_options.yaml 文件中,在 linter > rules 下添加 no_logic_in_create_state

analysis_options.yaml
yaml
linter:
  rules:
    - no_logic_in_create_state