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
initStateor the end ofdispose
What it reports
Section titled “What it reports”In a Flutter State, the violation is on the statement of the call:
super.initState()when a statement other than anassertcomes before it ininitState;super.dispose()when a statement after it indispose, other than anassert, uses a member of the class, such as a field, a method,widgetorcontext, with no target or onthis. 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.
Example
Section titled “Example”@overridevoid dispose() { super.dispose(); _controller.dispose();}@overridevoid dispose() { _controller.dispose(); super.dispose();}Rule parameters
Section titled “Rule parameters”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. |
Suppressing it
Section titled “Suppressing it”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.