Flutter bloc state manager

From curiositech/windags-skills · View interactive version

Flutter bloc state manager is an AI agent skill from the GitHub repository curiositech/windags-skills. Flutter state management expert with BLoC/Cubit, Riverpod, Provider, and Navigation 2.0 (GoRouter). Activate on: Flutter state management, BLoC pattern, Cubit, Riverpod, Provider, GetX, GoRouter, Flutter navigation, flutter_bloc. NOT for: React Native (use react-native-architect), SwiftUI (use swift

How to use this skill

Skill file

Flutter BLoC State Manager

Expert in Flutter state management with BLoC/Cubit, Riverpod, GoRouter navigation, and scalable architecture patterns.

Decision Points

State Solution Selection Tree

1. State Complexity Assessment:
   ├─ Simple data (boolean, enum, single value) 
   │  └─ Use: Cubit with copyWith pattern
   ├─ Event-driven logic (debounce, side effects, audit trail)
   │  └─ Use: BLoC with events/states
   ├─ Global app state with DI
   │  └─ Use: Riverpod StateNotifier
   └─ Widget-scoped state only
      └─ Use: Provider or setState

2. When choosing between BLoC packages:
   ├─ Need testing/debugging tools + state/event separation
   │  └─ flutter_bloc
   ├─ Want compile-time safety + automatic disposal
   │  └─ Riverpod
   ├─ Legacy codebase migration
   │  └─ Provider (then migrate to Cubit)
   └─ Simple state only
      └─ Cubit (via flutter_bloc)

3. Navigation Architecture Decision:
   ├─ Deep linking required OR type safety needed
   │  └─ GoRouter
   ├─ Nested navigation with bottom tabs
   │  └─ GoRouter with StatefulShellRoute
   ├─ Simple push/pop navigation only
   │  └─ Navigator.push (but prefer GoRouter for consistency)
   └─ Web app with URL routing
      └─ GoRouter (required)

Real-time Decision Matrix

| Scenario | Cubit | BLoC | Riverpod | Provider |

|----------|-------|------|----------|----------|

| Form validation | ✅ | ❌ | ✅ | ❌ |

| API loading states | ✅ | ⚠️ | ✅ | ⚠️ |

| Event sourcing needed | ❌ | ✅ | ❌ | ❌ |

| Global auth state | ⚠️ | ⚠️ | ✅ | ❌ |

| Widget-only state | ❌ | ❌ | ❌ | ✅ |

Failure Modes

1. **Mutable State Mutation**

2. **Listener Hell**

3. **Rebuild Cascades**

4. **God State Object**

5. **Navigation State Conflicts**

Worked Examples

Complete E-commerce Cart Implementation

Scenario: Build shopping cart with add/remove items, quantity updates, price calculations, and checkout navigation.

1. State Design (Expert catches: immutability, separation of concerns)

// Novice: Single cart state with mutable list
// Expert: Immutable state with calculated properties
class CartState extends Equatable {
  final Map<String, CartItem> items;  // Expert: Use Map for O(1) lookups
  final CartStatus status;
  
  // Expert: Calculated properties prevent state inconsistency
  double get totalPrice => items.values.fold(0, (sum, item) => sum + item.total);
  int get itemCount => items.values.fold(0, (sum, item) => sum + item.quantity);
  
  CartState copyWith({Map<String, CartItem>? items, CartStatus? status}) {
    return CartState._(items ?? this.items, status ?? this.status);
  }
}

2. Decision Point Navigation: Adding item logic

3. Implementation (Expert catches: performance, error handling)

class CartCubit extends Cubit<CartState> {
  Future<void> addItem(Product product, int quantity) async {
    final currentItems = Map<String, CartItem>.from(state.items);
    final existingItem = currentItems[product.id];
    
    if (existingItem != null) {
      // Expert: Immutable update of nested object
      currentItems[product.id] = existingItem.copyWith(
        quantity: existingItem.quantity + quantity
      );
    } else {
      currentItems[product.id] = CartItem.fromProduct(product, quantity);
    }
    
    emit(state.copyWith(items: currentItems));
    
    // Expert: Navigation coupling - delegate to router
    if (state.itemCount == 1) {
      context.go('/cart-overview');  // First item added
    }
  }
}

4. What Novice Misses vs Expert Catches:

Quality Gates

NOT-FOR Boundaries

This skill should NOT be used for:

Delegate to other skills when:

Use this skill with your AI agent: Open on Skill Issue to copy, download, or add to your library. Works with Claude, ChatGPT, Gemini, Cursor, Windsurf and more.