Skip to main content

avoid-state-constructors

added in: 1.4.0
Pro+
preset: recommended

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

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

Example

❌ Bad:

class _MyHomePageState<T> extends State<MyHomePage> {
// LINT: Avoid creating constructors for widgets. Try moving the constructor logic to the 'initState' method.
_MyHomePageState() {
// ...
}
}

✅ Good:

class _MyHomePageState<T> extends State<MyHomePage> {

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