Skip to main content

prefer-immutable-bloc-state

added in: 1.25.0
⚙️🛠
Free+

Warns when a Bloc state does not have the @immutable annotation.

Having immutable state objects helps ensure you always pass a newly create object to emit invocations and avoid any issues with the state not being updated.

info

If you keep your bloc state in separate files, consider using the include configuration option to scope the rule to only those files.

⚙️ Config

Set name-pattern (default is State$) to set a regular expression pattern for bloc state names.

analysis_options.yaml
dart_code_metrics:
rules:
- prefer-immutable-bloc-state:
name-pattern: State$

Example

❌ Bad:

// LINT: Prefer adding '@immutable' to Bloc state.
sealed class CounterState {}

// LINT: Prefer adding '@immutable' to Bloc state.
class CounterIncrement extends CounterState {}

✅ Good:


sealed class CounterState {}


class CounterIncrement extends CounterState {}