proper-super-init-state

Same as proper_super_init_state in Pyramid Lint

Path
lint/correctness/proper-super-init-state
Default
On (recommended)
Origin
Pyramid Lint
Domain
flutter
Rule
425 of 512
recommendedflutter

Require super.initState() to be the first statement of a resolved Flutter State subclass's initState override.

Invalid

example.dartdart
import 'package:flutter/material.dart';

class A extends State<StatefulWidget> {
  @override
  void initState() {
    doStuff();
    super.initState();
  }
}

class B extends State<StatefulWidget> {
  int x = 0;

  @override
  void initState() {
    x = 1;
  }
}

class C extends State<StatefulWidget> {
  @override
  void initState() {}
}

class D extends ConsumerState<StatefulWidget> {
  @override
  void initState() {
    final a = 1;
    super.initState();
    print(a);
  }
}

class E extends State<StatefulWidget> {
  @override
  void initState() {
    setup();
    super.initState();
  }
}
Call `super.initState()` first in `initState()`.
6 doStuff();
7 super.initState();
Call `super.initState()` first in `initState()`.
14 @override
15 void initState() {
Call `super.initState()` first in `initState()`.
21 @override
22 void initState() {}
Call `super.initState()` first in `initState()`.
37 setup();
38 super.initState();

Valid

example.dartdart
import 'package:flutter/material.dart';

class A extends State<StatefulWidget> {
  @override
  void initState() {
    super.initState();
    doStuff();
  }
}

class B extends State<StatefulWidget> {
  @override
  void initState() {
    super.initState();
  }
}

class C extends ConsumerState<StatefulWidget> {
  @override
  void initState() {
    super.initState();
    final a = 1;
    print(a);
  }
}

class D {
  void initState() {
    doStuff();
  }
}

class E extends StatelessWidget {
  void initState() {}
}

Configure proper-super-init-state

Set its severity in falcon.json. See the configuration guide.

falcon.jsonjson
{
  "linter": {
    "rules": {
      "correctness": {
        "proper-super-init-state": "error"
      }
    }
  }
}

Or silence one occurrence with a suppression comment; the reason is required.

example.dartdart
// falcon-ignore lint/correctness/proper-super-init-state: <reason>
  • require-super-disposerecommendedflutter

    Require super.dispose() in Flutter State dispose overrides.

  • sort-pub-dependenciessort_pub_dependenciescross-file

    Require pubspec.yaml dependency sections to be sorted alphabetically.

  • test-types-in-equalstest_types_in_equalsrecommended

    Flags casting the parameter of operator == instead of testing its type.

  • throw-in-finallythrow_in_finallyrecommended

    Flags throw inside a finally block.

  • type-literal-in-constant-patterntype_literal_in_constant_patternrecommended

    Don't use constant patterns whose expressions have the core Type type.

  • undeclared-dependenciescross-file

    Report package imports and exports missing from the enclosing pubspec.

  • unnecessary-flutter-importsrecommendedflutter

    Disallow unused Flutter framework and dart:async imports.

  • unnecessary-nullablecheck-unnecessary-nullablecross-file

    Flags private function or method parameters whose nullable type is unnecessary.