BLoC State Management
Priority: P0 (CRITICAL)
Structure
lib/features/auth/
├── bloc/
│ ├── auth_bloc.dart
│ ├── auth_event.dart # (@freezed or Equatable)
│ └── auth_state.dart # (@freezed or Equatable)
Implementation Guidelines
- States & Events: Use
@freezedfor union states. See references/bloc_templates.md. - Error Handling: Use
Failureobjects; avoid throwing exceptions. - Async Data: Use
emit.forEachfor streams. - Concurrency: Use
transformerfor event debouncing. - Testing: Use
blocTestfor state transition verification. - Injection: Register BLoCs as
@injectable(Factory).
Anti-Patterns
- No .then(): Use
awaitoremit.forEach()to emit states. - No Logic in Builder: Perform calculations in BLoC, not inside
BlocBuilder. - No BLoC-to-BLoC: Use streams to coordinate BLoCs, not direct references.
Related Topics
layer-based-clean-architecture | dependency-injection | error-handling