prefer-sealed-bloc-state

Path
lint/correctness/prefer-sealed-bloc-state
Default
On (recommended)
Origin
Falcon
Domain
bloc
Rule
404 of 512
recommendedbloc

Flags Bloc state classes without a sealed or final modifier.

Closed hierarchies make exhaustive handling reliable. Identifies roots from the generic arguments of package-confirmed Bloc or Cubit superclasses, then checks those roots and their resolved subclasses. Unrelated name suffixes and unresolved model types are ignored. Both sealed and final satisfy the rule. Roots and declarations must be in the current file; separate part files are not joined.

Invalid

example.dartdart
import 'package:bloc/bloc.dart';
import 'package:flutter_bloc/flutter_bloc.dart' as fb;
import 'package:meta/meta.dart';

abstract class Snapshot {}
class First extends Snapshot {}
class Second extends Snapshot {}
class Deep extends First {}
class Other {}
class Example extends Bloc<int, Snapshot> { Example() : super(First()); }
class Another extends fb.Cubit<Other> { Another() : super(Other()); }

class UnrelatedEvent {}
class UnrelatedState {}
Declare this Bloc state `sealed` or `final`.
4
5abstract class Snapshot {}
Declare this Bloc state `sealed` or `final`.
5abstract class Snapshot {}
6class First extends Snapshot {}
Declare this Bloc state `sealed` or `final`.
6class First extends Snapshot {}
7class Second extends Snapshot {}
Declare this Bloc state `sealed` or `final`.
7class Second extends Snapshot {}
8class Deep extends First {}
Declare this Bloc state `sealed` or `final`.
8class Deep extends First {}
9class Other {}

Valid

example.dartdart
import 'package:bloc/bloc.dart';
import 'package:meta/meta.dart';

sealed class Snapshot {}
final class First extends Snapshot {}
final class Second extends Snapshot {}
class Example extends Bloc<int, Snapshot> { Example() : super(First()); }
final class Other {}
class Another extends Cubit<Other> { Another() : super(Other()); }

class UnrelatedEvent {}
class UnrelatedState {}
class NoSuffix {}

Configure prefer-sealed-bloc-state

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

falcon.jsonjson
{
  "linter": {
    "rules": {
      "correctness": {
        "prefer-sealed-bloc-state": "error"
      }
    }
  }
}

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

example.dartdart
// falcon-ignore lint/correctness/prefer-sealed-bloc-state: <reason>
  • prefer-void-to-nullprefer_void_to_nullrecommended

    Flags uses of the Null type where void is meant.

  • proper-controller-disposeproper_controller_disposerecommendedflutter

    Requires Flutter controllers owned by a State field to be disposed.

  • proper-expanded-and-flexibleproper_expanded_and_flexiblerecommendedflutter

    Require Expanded and Flexible to sit directly inside a flex widget.

  • proper-from-environmentproper_from_environmentrecommended

    Require fromEnvironment to be evaluated in a const context.

  • proper-super-init-stateproper_super_init_staterecommendedflutter

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

  • 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.