Skip to main content

avoid-state-constructors

added in: 1.4.0
preset: recommended

Warns when a State has a constructor with non-empty body.

The constructor of a State object should never contain any logic. That logic should always go into State.initState.

Example

❌ Bad:

class _MyHomePageState<T> extends State<MyHomePage> {
_MyHomePageState() {
// ... // LINT
}
}

✅ Good:

class _MyHomePageState<T> extends State<MyHomePage> {

void initState() {
// ...
}
}