Dart/Flutter Patterns
When to Use
Use this skill when:
- Starting a new Flutter feature and need idiomatic patterns for state management, navigation, or data access
- Reviewing or writing Dart code and need guidance on null safety, sealed types, or async composition
- Setting up a new Flutter project and choosing between BLoC, Riverpod, or Provider
- Implementing secure HTTP clients, WebView integration, or local storage
- Writing tests for Flutter widgets, Cubits, or Riverpod providers
- Wiring up GoRouter with authentication guards
How It Works
Copy-paste-ready patterns by concern (full code in references/):
- Null safety — avoid
!, prefer ?./??/pattern matching (foundations.md)
- Immutable state — sealed classes,
freezed, copyWith (foundations.md)
- Async composition — concurrent
Future.wait, safe BuildContext after await (foundations.md)
- Widget architecture — extract to classes,
const propagation, scoped rebuilds (foundations.md)
- State management — BLoC/Cubit events, Riverpod notifiers + derived providers (
state-management.md)
- Navigation — GoRouter with reactive auth guards via
refreshListenable (navigation-networking.md)
- Networking — Dio with interceptors, token refresh with one-time retry guard (
navigation-networking.md)
- Error handling — global capture,
ErrorWidget.builder, crashlytics (error-testing.md)
- Testing — unit (BLoC test), widget (ProviderScope overrides), fakes over mocks (
error-testing.md)
Examples
// Sealed state — prevents impossible states
sealed class AsyncState<T> {}
final class Loading<T> extends AsyncState<T> {}
final class Success<T> extends AsyncState<T> { final T data; const Success(this.data); }
final class Failure<T> extends AsyncState<T> { final Object error; const Failure(this.error); }
// GoRouter with reactive auth redirect
final router = GoRouter(
refreshListenable: GoRouterRefreshStream(authCubit.stream),
redirect: (context, state) {
final authed = context.read<AuthCubit>().state is AuthAuthenticated;
if (!authed && !state.matchedLocation.startsWith('/login')) return '/login';
return null;
},
routes: [...],
);
// Riverpod derived provider with safe firstWhereOrNull
@riverpod
double cartTotal(Ref ref) {
final cart = ref.watch(cartNotifierProvider);
final products = ref.watch(productsProvider).valueOrNull ?? [];
return cart.fold(0.0, (total, item) {
final product = products.firstWhereOrNull((p) => p.id == item.productId);
return total + (product?.price ?? 0) * item.quantity;
});
}
1---2name: dart-flutter-patterns3description: "Use when production-ready Dart and Flutter patterns covering null safety, immutable state, async composition, widget architecture, popular state management frameworks (BLoC, Riverpod, Provider), GoRouter navigation, Dio networking, Freezed code generation,... Triggers on \"dart-flutter-patterns\", \"dart flutter patterns\", \"patterns\"."4metadata:5 source: https://skillmd.com/skills/majinmagros/dart-flutter-patterns6 origin: ECC7---89# Dart/Flutter Patterns1011## When to Use1213Use this skill when:14- Starting a new Flutter feature and need idiomatic patterns for state management, navigation, or data access15- Reviewing or writing Dart code and need guidance on null safety, sealed types, or async composition16- Setting up a new Flutter project and choosing between BLoC, Riverpod, or Provider17- Implementing secure HTTP clients, WebView integration, or local storage18- Writing tests for Flutter widgets, Cubits, or Riverpod providers19- Wiring up GoRouter with authentication guards2021## How It Works2223Copy-paste-ready patterns by concern (full code in `references/`):24251. **Null safety** — avoid `!`, prefer `?.`/`??`/pattern matching (`foundations.md`)262. **Immutable state** — sealed classes, `freezed`, `copyWith` (`foundations.md`)273. **Async composition** — concurrent `Future.wait`, safe `BuildContext` after `await` (`foundations.md`)284. **Widget architecture** — extract to classes, `const` propagation, scoped rebuilds (`foundations.md`)295. **State management** — BLoC/Cubit events, Riverpod notifiers + derived providers (`state-management.md`)306. **Navigation** — GoRouter with reactive auth guards via `refreshListenable` (`navigation-networking.md`)317. **Networking** — Dio with interceptors, token refresh with one-time retry guard (`navigation-networking.md`)328. **Error handling** — global capture, `ErrorWidget.builder`, crashlytics (`error-testing.md`)339. **Testing** — unit (BLoC test), widget (ProviderScope overrides), fakes over mocks (`error-testing.md`)3435## Examples3637```dart38// Sealed state — prevents impossible states39sealed class AsyncState<T> {}40final class Loading<T> extends AsyncState<T> {}41final class Success<T> extends AsyncState<T> { final T data; const Success(this.data); }42final class Failure<T> extends AsyncState<T> { final Object error; const Failure(this.error); }4344// GoRouter with reactive auth redirect45final router = GoRouter(46 refreshListenable: GoRouterRefreshStream(authCubit.stream),47 redirect: (context, state) {48 final authed = context.read<AuthCubit>().state is AuthAuthenticated;49 if (!authed && !state.matchedLocation.startsWith('/login')) return '/login';50 return null;51 },52 routes: [...],53);5455// Riverpod derived provider with safe firstWhereOrNull56@riverpod57double cartTotal(Ref ref) {58 final cart = ref.watch(cartNotifierProvider);59 final products = ref.watch(productsProvider).valueOrNull ?? [];60 return cart.fold(0.0, (total, item) {61 final product = products.firstWhereOrNull((p) => p.id == item.productId);62 return total + (product?.price ?? 0) * item.quantity;63 });64}65```