project-bloc

gsmlg-app's avatarfrom gsmlg-app

Guide for creating BLoC patterns with events, states, and proper structure in app_bloc packages (project)

⭐1stars🔀0forks📁View on GitHub🕐Updated Jan 7, 2026⬇️1downloads

When & Why to Use This Skill

The Flutter BLoC Development Skill is a comprehensive guide and automation tool for implementing the Business Logic Component (BLoC) pattern in Flutter applications. It leverages Mason templates to generate standardized code for simple states, lists, and forms, ensuring architectural consistency, improved maintainability, and robust testing across the app_bloc package structure.

Use Cases

  • Standardizing State Management: Automatically generate BLoC structures for new features to maintain a consistent architecture across large-scale Flutter projects.
  • Accelerating Development with Mason: Use pre-defined templates to quickly scaffold BLoC components for forms, lists, and basic business logic, significantly reducing manual boilerplate code.
  • Enforcing Best Practices: Implement proper event naming, state status enums, and immutable updates using copyWith() as defined in the project's specific conventions.
  • Automated Testing Setup: Ensure every new BLoC includes a corresponding test suite in the test/ directory to verify initial states, transitions, and error handling.
nameproject-bloc
descriptionGuide for creating BLoC patterns with events, states, and proper structure in app_bloc packages (project)

Flutter BLoC Development Skill

This skill guides the creation of BLoC (Business Logic Component) packages following this project's conventions.

When to Use

Trigger this skill when:

  • Creating a new BLoC package
  • Adding events or states to an existing BLoC
  • Implementing business logic that manages application state
  • User asks to "create a bloc", "add state management", or "implement feature with BLoC"

Mason Templates Available

Always use Mason templates first:

# Simple BLoC (basic state management)
mason make simple_bloc -o app_bloc/feature_name --name=feature_name

# List BLoC (for managing lists with CRUD operations)
mason make list_bloc -o app_bloc/feature_name --name=feature_name

# Form BLoC (for form validation and submission)
mason make form_bloc --name Login --field_names "email,password"

Project Structure

BLoC packages MUST be created in app_bloc/:

app_bloc/
└── feature_name/
    ├── lib/
    │   ├── feature_name_bloc.dart  # Barrel export file
    │   └── src/
    │       ├── bloc.dart           # BLoC class with event handlers
    │       ├── event.dart          # Event definitions
    │       └── state.dart          # State class with status enum
    ├── test/
    │   └── feature_name_bloc_test.dart
    └── pubspec.yaml

BLoC Pattern Conventions

Event Naming

  • Events are named as actions: LoadData, SubmitForm, ChangeTheme
  • Use past tense for completed actions: DataLoaded, FormSubmitted

State Structure

  • States use a status enum: initial, loading, completed, error
  • Use copyWith() for immutable state updates
  • Extend Equatable for proper state comparison

Event Handlers

  • Prefix handlers with _on: _onLoadData, _onSubmitForm
  • Use Emitter<State> for state emissions
  • Handle errors with try-catch and emit error states

Reference Implementation

ThemeBloc (app_bloc/theme/) serves as the reference implementation:

class ThemeBloc extends Bloc<ThemeEvent, ThemeState> {
  ThemeBloc(this.pref) : super(ThemeState.initial(pref)) {
    on<ChangeTheme>(_onThemeChanged);
    on<ChangeThemeMode>(_onThemeModeChanged);
  }

  Future<void> _onThemeChanged(
    ChangeTheme event,
    Emitter<ThemeState> emitter,
  ) async {
    emitter(state.copyWith(theme: event.theme));
    pref.setString('themeName', event.theme.name);
  }
}

Dependencies

In pubspec.yaml, use workspace resolution:

dependencies:
  bloc: any
  flutter_bloc: any
  equatable: any

Testing Requirements

Every BLoC MUST have tests in test/ directory:

  • Test initial state
  • Test each event handler
  • Test state transitions
  • Test error handling

Run tests: cd app_bloc/feature_name && flutter test

Integration with UI

Use BlocProvider and BlocBuilder from flutter_bloc:

BlocProvider(
  create: (context) => FeatureBloc(),
  child: BlocBuilder<FeatureBloc, FeatureState>(
    builder: (context, state) {
      // Build UI based on state
    },
  ),
)