custom-plugin-flutter-skill-state
2300+ lines of state management mastery - all patterns (Provider, Riverpod, BLoC, GetX), dependency injection, persistence, testing with production-ready code examples.
What this skill does
# custom-plugin-flutter: State Management Skill
## Quick Start - Provider Pattern
```dart
// Define notifier
class CounterNotifier extends ChangeNotifier {
int _count = 0;
int get count => _count;
void increment() {
_count++;
notifyListeners();
}
void decrement() {
_count--;
notifyListeners();
}
}
// Create provider
final counterProvider = ChangeNotifierProvider((ref) => CounterNotifier());
// Use in UI
class CounterApp extends ConsumerWidget {
@override
Widget build(BuildContext context, WidgetRef ref) {
final counter = ref.watch(counterProvider);
return Scaffold(
body: Center(
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: [
Text('Count: ${counter.count}'),
Row(
mainAxisAlignment: MainAxisAlignment.center,
children: [
ElevatedButton(
onPressed: () => counter.decrement(),
child: Text('-'),
),
SizedBox(width: 16),
ElevatedButton(
onPressed: () => counter.increment(),
child: Text('+'),
),
],
),
],
),
),
);
}
}
```
## 1. setState Foundation
**When setState Suffices:**
```dart
class SimpleCounter extends StatefulWidget {
@override
State<SimpleCounter> createState() => _SimpleCounterState();
}
class _SimpleCounterState extends State<SimpleCounter> {
int _count = 0;
@override
Widget build(BuildContext context) {
return Scaffold(
body: Center(
child: Column(
children: [
Text('Count: $_count'),
ElevatedButton(
onPressed: () => setState(() => _count++),
child: Text('Increment'),
),
],
),
),
);
}
}
```
**setState Limitations:**
- ❌ Rebuilds entire widget subtree
- ❌ No code generation support
- ❌ Hard to test business logic
- ❌ Doesn't work across widgets
## 2. Provider Ecosystem
**ChangeNotifier Pattern:**
```dart
class UserService extends ChangeNotifier {
User? _user;
User? get user => _user;
Future<void> fetchUser(String id) async {
try {
_user = await api.getUser(id);
notifyListeners();
} catch (e) {
// Error handling
}
}
}
final userProvider = ChangeNotifierProvider((ref) => UserService());
// In UI
Consumer(
builder: (context, ref, child) {
final user = ref.watch(userProvider).user;
return user != null ? Text(user.name) : Text('Loading...');
},
)
```
**FutureProvider for Async:**
```dart
final userProvider = FutureProvider<User>((ref) async {
return await api.getUser('123');
});
// Usage
ref.watch(userProvider).when(
loading: () => CircularProgressIndicator(),
data: (user) => Text(user.name),
error: (error, st) => Text('Error: $error'),
)
```
## 3. Riverpod Modern Approach
**Functional State Management:**
```dart
// Simple provider
final nameProvider = StateProvider((ref) => 'John');
// FutureProvider for async operations
final userProvider = FutureProvider.family<User, String>((ref, id) async {
return await api.getUser(id);
});
// StateNotifierProvider for complex state
final counterProvider = StateNotifierProvider<CounterNotifier, int>((ref) {
return CounterNotifier();
});
class CounterNotifier extends StateNotifier<int> {
CounterNotifier() : super(0);
void increment() => state++;
void decrement() => state--;
}
// Usage
Consumer(
builder: (context, ref, child) {
final count = ref.watch(counterProvider);
return ElevatedButton(
onPressed: () => ref.read(counterProvider.notifier).increment(),
child: Text('Count: $count'),
);
},
)
```
## 4. BLoC Pattern
**Complete BLoC Example:**
```dart
// Events
abstract class UserEvent {}
class GetUserEvent extends UserEvent {
final String id;
GetUserEvent(this.id);
}
// States
abstract class UserState {}
class UserLoading extends UserState {}
class UserLoaded extends UserState {
final User user;
UserLoaded(this.user);
}
class UserError extends UserState {
final String error;
UserError(this.error);
}
// BLoC
class UserBloc extends Bloc<UserEvent, UserState> {
final UserRepository _repository;
UserBloc(this._repository) : super(UserLoading()) {
on<GetUserEvent>(_onGetUser);
}
Future<void> _onGetUser(GetUserEvent event, Emitter<UserState> emit) async {
emit(UserLoading());
try {
final user = await _repository.getUser(event.id);
emit(UserLoaded(user));
} catch (e) {
emit(UserError(e.toString()));
}
}
}
// UI Integration
BlocBuilder<UserBloc, UserState>(
builder: (context, state) {
if (state is UserLoading) return CircularProgressIndicator();
if (state is UserLoaded) return Text(state.user.name);
if (state is UserError) return Text('Error: ${state.error}');
return SizedBox();
},
)
```
## 5. Dependency Injection
**GetIt Service Locator:**
```dart
final getIt = GetIt.instance;
// Setup
void setupServiceLocator() {
getIt.registerSingleton<ApiClient>(ApiClient());
getIt.registerSingleton<UserRepository>(
UserRepository(getIt<ApiClient>()),
);
getIt.registerLazySingleton<UserBloc>(
() => UserBloc(getIt<UserRepository>()),
);
}
// Usage
final userBloc = getIt<UserBloc>();
```
## 6. State Persistence
**Hydration Pattern:**
```dart
class UserBloc extends Bloc<UserEvent, UserState> {
UserBloc(this._repository) : super(const UserInitial()) {
on<GetUserEvent>(_onGetUser);
}
@override
Future<void> close() {
_savePersistentState();
return super.close();
}
Future<void> _savePersistentState() async {
final box = await Hive.openBox('userBloc');
await box.put('state', state.toJson());
}
Future<void> loadPersistedState() async {
final box = await Hive.openBox('userBloc');
final json = box.get('state');
if (json != null) {
emit(UserState.fromJson(json));
}
}
}
```
## 7. Testing State Management
**Testing with bloc_test:**
```dart
void main() {
group('CounterBloc', () {
late CounterBloc counterBloc;
setUp(() {
counterBloc = CounterBloc();
});
tearDown(() {
counterBloc.close();
});
blocTest<CounterBloc, int>(
'emits [1] when CounterIncrementPressed is added',
build: () => counterBloc,
act: (bloc) => bloc.add(CounterIncrementPressed()),
expect: () => [1],
);
});
}
```
## 8. Advanced Patterns
**Derived State with Selectors:**
```dart
final filteredUsersProvider = Provider<List<User>>((ref) {
final users = ref.watch(usersProvider);
final filter = ref.watch(filterProvider);
return users.where((u) => u.name.contains(filter)).toList();
});
// Usage
Consumer(
builder: (context, ref, child) {
final filtered = ref.watch(filteredUsersProvider);
return ListView.builder(
itemCount: filtered.length,
itemBuilder: (context, index) => Text(filtered[index].name),
);
},
)
```
## 9. Performance Optimization
- ✅ Use `Selector` for fine-grained rebuilds
- ✅ Memoize expensive computations
- ✅ Lazy initialize expensive services
- ✅ Profile with DevTools regularly
- ✅ Use `const` widgets throughout
## 10. Pattern Selection
| Complexity | Pattern | When |
|-----------|---------|------|
| Trivial | setState | Single widget |
| Simple | Provider | Multiple screens |
| Medium | Riverpod | Type-safe needed |
| Complex | BLoC | Enterprise scale |
---
**Master state management for scalable Flutter applications.**
Related in General
modeling-omnistudio-epc-catalog
IncludedSalesforce Industries CME EPC product-modeling skill for Product2-based catalog creation. Use when creating EPC products, configuring product attributes, building offer bundles with Product Child Items, or reviewing EPC DataPack JSON metadata for product catalog changes. TRIGGER when: user creates or updates Product2 EPC records, AttributeAssignment payloads, AttributeMetadata/AttributeDefaultValues, Offer bundles, or ProductChildItem relationships. DO NOT TRIGGER when: designing OmniScripts/FlexCards/Integration Procedures (use building-omnistudio-omniscript, building-omnistudio-flexcard, or building-omnistudio-integration-procedure), implementing Apex business logic (use generating-apex), or troubleshooting deployment pipelines (use deploying-metadata).
relationship-science-coach
IncludedUse this skill for direct, practical adult relationship coaching: couples conflict, repair, trust, marriage, dating, flirting, attachment patterns, emotional connection, sex, desire differences, eroticism, kink negotiation, affection, love languages, breakups, and long-term passion. Draw on Gottman, EFT and Hold Me Tight, attachment science, modern sex research, Perel, Nagoski, Kerner, Schnarch, Love and Stosny, and flexible love-language tools. Be concrete and low-hedge. Redirect only for imminent danger, abuse, coercive control, minors, non-consent, self-harm, stalking, or medical/legal/psychiatric decisions.
building-sf-integrations
IncludedSalesforce integration architecture and runtime plumbing with 120-point scoring. Use this skill to set up Named Credentials, External Credentials, External Services, REST/SOAP callout patterns, Platform Events, and Change Data Capture. TRIGGER when: user sets up Named Credentials, External Services, REST/SOAP callouts, Platform Events, CDC, or touches .namedCredential-meta.xml files. DO NOT TRIGGER when: Connected App/OAuth config (use configuring-connected-apps), Apex-only logic (use generating-apex), or data import/export (use handling-sf-data).
venue-templates
IncludedAccess comprehensive LaTeX templates, formatting requirements, and submission guidelines for major scientific publication venues (Nature, Science, PLOS, IEEE, ACM), academic conferences (NeurIPS, ICML, CVPR, CHI), research posters, and grant proposals (NSF, NIH, DOE, DARPA). This skill should be used when preparing manuscripts for journal submission, conference papers, research posters, or grant proposals and need venue-specific formatting requirements and templates.
let-fate-decide
IncludedDraws the 12 Houses of the Zodiac Tarot spread to inject entropy into planning when prompts are vague, ambiguous, or casually delegated. Interprets the spread to guide next steps. Use when the user says 'let fate decide', 'YOLO', 'whatever', 'idk', or other nonchalant phrases, makes Yu-Gi-Oh references, or when you are about to arbitrarily pick between multiple reasonable approaches. Prefer over ask-questions-if-underspecified when the user's tone is casual or playful rather than precision-seeking.
net-ops
IncludedCross-platform network troubleshooting (Windows, macOS, Linux) via local or remote shell. Use for: DNS broken, can't resolve hostnames, nslookup/dig works but apps fail, NRPT, WFP, scutil, /etc/resolver, systemd-resolved, /etc/resolv.conf, NetworkManager, VPN DNS leak residue (ProtonVPN/Mullvad/WireGuard/AnyConnect), AV/firewall blocking DNS or DoH, Tailscale DNS interaction, intermittent connectivity, remote diagnostics over SSH.