prefer-immutable-bloc-events

Path
lint/correctness/prefer-immutable-bloc-events
Default
On (recommended)
Origin
Falcon
Domain
bloc
Rule
369 of 512
recommendedbloc

Flags Bloc event classes without an explicit @immutable annotation.

Immutable models keep state changes predictable and discourage accidental mutation. Identifies roots from the generic arguments of package-confirmed Bloc superclasses, then checks those roots and their resolved subclasses. Unrelated name suffixes and unresolved model types are ignored. Each class must carry @immutable or @Immutable itself; inheritance is not enough. 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 Action {}
class First extends Action {}
class Second extends Action {}
class Deep extends First {}
class Other {}
class Example extends Bloc<Action, int> { Example() : super(0); }
class Another extends fb.Bloc<Other, int> { Another() : super(0); }

class UnrelatedEvent {}
class UnrelatedState {}

@immutable
class Annotated extends Action {}
class MissingOwnAnnotation extends Annotated {}
Annotate this Bloc event with `@immutable`.
4
5abstract class Action {}
Annotate this Bloc event with `@immutable`.
5abstract class Action {}
6class First extends Action {}
Annotate this Bloc event with `@immutable`.
6class First extends Action {}
7class Second extends Action {}
Annotate this Bloc event with `@immutable`.
7class Second extends Action {}
8class Deep extends First {}
Annotate this Bloc event with `@immutable`.
8class Deep extends First {}
9class Other {}
Annotate this Bloc event with `@immutable`.
17class Annotated extends Action {}
18class MissingOwnAnnotation extends Annotated {}

Valid

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

@immutable
sealed class Action {}
@immutable
final class First extends Action {}
@immutable
final class Second extends Action {}
class Example extends Bloc<Action, int> { Example() : super(0); }
@immutable
final class Other {}
class Another extends Bloc<Other, int> { Another() : super(0); }

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

Configure prefer-immutable-bloc-events

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

falcon.jsonjson
{
  "linter": {
    "rules": {
      "correctness": {
        "prefer-immutable-bloc-events": "error"
      }
    }
  }
}

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

example.dartdart
// falcon-ignore lint/correctness/prefer-immutable-bloc-events: <reason>