Skip to content

lifecycle_super_call_order

Reports a super.initState() that is not first in initState, and a super.dispose() followed by code that still uses the state. Move the call to the start of initState or to the end of dispose.

Tier
advisory
Scope
Flutter
Lowest preset
recommended
Fix
moves the call to the start of initState or the end of dispose

In a Flutter State, the violation is on the statement of the call:

  • super.initState() when a statement other than an assert comes before it in initState;
  • super.dispose() when a statement after it in dispose, other than an assert, uses a member of the class, such as a field, a method, widget or context, with no target or on this. A log line after it is fine.

Only a call that is a statement of the body itself is looked at. A method that leaves the call out is the analyzer’s to report.

The fix moves the statement. It is not offered where the call, or the first or last statement, shares its line with other code, or where a return in dispose would skip the moved call.

The rule is silent in a package that does not use Flutter.

What the state inherits, from Flutter and from its mixins such as a ticker provider, is set up by super.initState() and torn down by super.dispose(). Code before the first may use what is not set up yet, and code after the second what is already torn down.

Violating
@override
void dispose() {
super.dispose();
_controller.dispose();
}
Corrected
@override
void dispose() {
_controller.dispose();
super.dispose();
}

Set under the assay: key of the project’s root analysis_options.yaml.

Rule parameter Default Effect
enabled true false switches the rule off for the whole project.
exclude [] Path globs where the rule is silent.

Write why on the line directly above the suppression:

// Why this place is fine.
// ignore: lifecycle_super_call_order

// ignore_for_file: assay/lifecycle_super_call_order silences it in a whole file, and // ignore: assay/lifecycle_super_call_order with the prefix works too. See Suppressions.