state-management
Original:🇺🇸 English
Translated
State Management, Dependency Injection & Navigation
12installs
Sourcedhruvanbhalara/skills
Added on
NPX Install
npx skill4agent add dhruvanbhalara/skills state-managementTags
Translated version includes tags in frontmatterSKILL.md Content
View Translation Comparison →BLoC Pattern
- Sealed States & Events: Always use for both States and Events to ensure exhaustive UI handling and compile-time safety.
sealed class - Immutability: All States, Events, and Domain Entities MUST be immutable (using and
finalorEquatable).freezed - Official BLoC Part-Part Of Pattern: Every file MUST include its corresponding
_bloc.dartand_event.dartfiles using_state.dartdirectives. Each event/state file MUST have apartdirective pointing back to the bloc file. This ensures a single library scope and shared private members.part ofdart// auth_bloc.dart part 'auth_event.dart'; part 'auth_state.dart'; class AuthBloc extends Bloc<AuthEvent, AuthState> { ... } // auth_event.dart part of 'auth_bloc.dart'; // auth_state.dart part of 'auth_bloc.dart'; - Mandatory Directory Structure: Every BLoC feature set MUST reside in its own sub-directory within the folder. Flat
bloc/directories are STRICTLY prohibited.bloc/textpresentation/bloc/ └── <bloc_name>/ ├── <bloc_name>_bloc.dart ├── <bloc_name>_event.dart └── <bloc_name>_state.dart - Loading State Mandate: ALWAYS emit before async work, then
LoadingorSuccess. Never skip the loading state.Error - Concurrency: Use (e.g.,
transformers,restartable()) for events requiring debouncing (search) or throttling (buttons).droppable() - Zero-Logic UI: Widgets MUST NOT contain business logic, orchestration logic, or direct calls to external services. They should ONLY dispatch events and build UI based on BLoC states.
BLoC Widget Usage
- for local UI rebuilds based on state
BlocBuilder - for side effects (navigation, snackbars, dialogs)
BlocListener - when both rebuild and side effects are needed
BlocConsumer - for dispatching events
context.read<Bloc>().add(Event()) - for reactive rebuilds (inside
context.watch<Bloc>().stateonly)build()
BLoC Submission Checklist
- Events and States use with correct
Equatableprops - All async operations follow pattern
Loading → Success/Error - No business logic in UI widgets
- No SDK/API calls outside DataSources
- Zero hardcoded colors, spacing, or typography \u2014 use design tokens (,
AppColors)AppSpacing - Code formatted with
dart format
Dependency Injection
- Use for dependency injection and service location
injectable - Standardized Injection:
- Use for screen-specific BLoCs to ensure a fresh instance per screen access.
@injectable - Use for global or shared BLoCs (e.g.,
@lazySingleton,AuthBloc,ThemeBloc,SettingsBloc).PasswordBloc
- Use
- Organize blocs logically by feature and ensure strict separation of concerns
Navigation & Routing
- Dynamic Routes: STRICTLY prohibit hardcoded route strings in configuration. Use static constants in
GoRouter.AppRoutes - Centralized BLoCs: BLoC providers MUST be injected via or
ShellRouteinBlocProviderwhen shared across multiple screens or within a feature branch.app_router.dart - No Local Providers: Avoid in individual screen
BlocProvidermethods if the BLoC is needed by a feature set.build() - Primitive Route Arguments: STRICTLY prohibit passing complex objects (BLoCs, ChangeNotifiers, Entity instances) as route arguments. Pass only primitive IDs/Keys and fetch data in the destination screen using or
Repositoryinjection.Bloc