Communitygithub.com

dart-flutter-patterns

涵蓋空值安全、不可變狀態、非同步組合、Widget 架構、主流狀態管理框架 (BLoC、Riverpod、Provider)、GoRouter 導航、Dio 網路層、Freezed 程式碼生成及簡潔架構的 Dart 與 Flutter 實戰模式。適用於撰寫或審查 Dart 及 Flutter 程式碼 — 包括狀態、Widget、導航、網路或架構。

dart-flutter-patterns 是什麼?

此技能提供了一系列適用於 Dart 和 Flutter 應用程式的生產級模式。其設計目的是在開發者需要時,提供即時可複製的程式碼範例和架構指南,適用的場景包括啟動新功能、審查程式碼或設定新專案。該技能明確支援多種生態系套件,如 BLoC、Riverpod、Provider (狀態管理)、GoRouter (導航)、Dio (網路請求) 和 Freezed (不可變資料類別)。模式涵蓋了從核心語言功能 (如空值安全與非同步組合) 到進階架構模式 (如錯誤處理與 Widget 測試) 的各個層面。透過提供實務且符合慣例的替代方案,該技能旨在幫助開發者避免常見陷阱 (例如過度使用 `!` 運算子或 `late` 關鍵字),並提升程式碼品質與可維護性。

相容平台~Claude Code~Codex CLI~Cursor
npx skills add https://github.com/affaan-m/everything-claude-code/tree/main/skills/dart-flutter-patterns

在你喜歡的 AI 中提問

開啟一個已預先載入此 Agent Skill 的新對話。

說明文件

Dart/Flutter 模式

使用時機

當發生以下情況時,請使用此技能:

  • 開始一個新的 Flutter 功能,需要狀態管理、導航或資料存取的慣用模式
  • 審查或編寫 Dart 程式碼,需要空值安全、密封類型或非同步組合的指導
  • 設定新的 Flutter 專案,並在 BLoC、Riverpod 或 Provider 之間做選擇
  • 實作安全的 HTTP 客戶端、WebView 整合或本地儲存
  • 為 Flutter Widget、Cubit 或 Riverpod Provider 編寫測試
  • 使用帶有身份驗證守護的 GoRouter 進行連線設定

運作方式

此技能提供按關注點組織的、可即時複製貼上的 Dart/Flutter 程式碼模式:

  1. 空值安全 — 避免 !,偏好使用 ?./??/模式匹配
  2. 不可變狀態 — 密封類別、freezed、copyWith
  3. 非同步組合 — 並行 Future.wait、await 後的安全 BuildContext
  4. Widget 架構 — 提取為類別 (而非方法)、const 傳播、作用域重建
  5. 狀態管理 — BLoC/Cubit 事件、Riverpod notifier 和衍生 Provider
  6. 導航 — 使用帶有 refreshListenable 的反應式身份驗證守護的 GoRouter
  7. 網路層 — 帶有攔截器的 Dio、帶有一次重試保護的 Token 刷新
  8. 錯誤處理 — 全域捕獲、ErrorWidget.builder、Crashlytics 接線
  9. 測試 — 單元測試 (BLoC 測試)、Widget 測試 (ProviderScope 覆蓋)、使用 Fake 而非 Mock

範例

// 密封狀態 — 防止不可能的狀態
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
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: [...],
);

// 帶有安全 firstWhereOrNull 的 Riverpod 衍生 Provider
@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;
  });
}

適用於 Dart 和 Flutter 應用程式的實戰、生產級模式。盡可能與函式庫無關,並明確涵蓋最常見的生態系套件。


1. 空值安全基礎

優先使用模式而非強制拆包運算子

// 不好 — 如果為 null 則在執行時崩潰
final name = user!.name;

// 好 — 提供備用值
final name = user?.name ?? 'Unknown';

// 好 — Dart 3 模式匹配 (針對複雜情況優先使用)
final display = switch (user) {
  User(:final name, :final email) => '$name <$email>',
  null => 'Guest',
};

// 好 — 提前返回保護
String getUserName(User? user) {
  if (user == null) return 'Unknown';
  return user.name; // 檢查後提升為非空
}

避免過度使用 late

// 不好 — 將空值錯誤推遲到執行時
late String userId;

// 好 — 使用可空類型並明確初始化
String? userId;

// 可以 — 僅在首次訪問前保證初始化時使用 late
// (例如在 initState() 中,在任何 Widget 互動之前)
late final AnimationController _controller;

@override
void initState() {
  super.initState();
  _controller = AnimationController(vsync: this, duration: const Duration(milliseconds: 300));
}

2. 不可變狀態

用於狀態層級的密封類別

sealed class UserState {}

final class UserInitial extends UserState {}

final class UserLoading extends UserState {}

final class UserLoaded extends UserState {
  const UserLoaded(this.user);
  final User user;
}

final class UserError extends UserState {
  const UserError(this.message);
  final String message;
}

// 詳盡的 switch — 編譯器強制所有分支
Widget buildFrom(UserState state) => switch (state) {
  UserInitial() => const SizedBox.shrink(),
  UserLoading() => const CircularProgressIndicator(),
  UserLoaded(:final user) => UserCard(user: user),
  UserError(:final message) => ErrorText(message),
};

使用 Freezed 實現無樣板不可變性

import 'package:freezed_annotation/freezed_annotation.dart';

part 'user.freezed.dart';
part 'user.g.dart';

@freezed
class User with _$User {
  const factory User({
    required String id,
    required String name,
    required String email,
    @Default(false) bool isAdmin,
  }) = _User;

  factory User.fromJson(Map<String, dynamic> json) => _$UserFromJson(json);
}

// 使用方式
final user = User(id: '1', name: 'Alice', email: '[email protected]');
final updated = user.copyWith(name: 'Alice Smith'); // 不可變更新
final json = user.toJson();
final fromJson = User.fromJson(json);

3. 非同步組合

使用 Future.wait 實現結構化並行

Future<DashboardData> loadDashboard(UserRepository users, OrderRepository orders) async {
  // 並行執行 — 不要順序 await
  final (userList, orderList) = await (
    users.getAll(),
    orders.getRecent(),
  ).wait; // Dart 3 記錄解構 + Future.wait 擴展

  return DashboardData(users: userList, orders: orderList);
}

Stream 模式

// Repository 為即時資料暴露反應式串流
Stream<List<Item>> watchCartItems() => _db
    .watchTable('cart_items')
    .map((rows) => rows.map(Item.fromRow).toList());

// 在 Widget 層 — 宣告式,無需手動訂閱
StreamBuilder<List<Item>>(
  stream: cartRepository.watchCartItems(),
  builder: (context, snapshot) => switch (snapshot) {
    AsyncSnapshot(connectionState: ConnectionState.waiting) =>
        const CircularProgressIndicator(),
    AsyncSnapshot(:final error?) => ErrorWidget(error.toString()),
    AsyncSnapshot(:final data?) => CartList(items: data),
    _ => const SizedBox.shrink(),
  },
)

await 後的 BuildContext

// 關鍵 — 在 StatefulWidget 中的任何 await 之後務必檢查 mounted
Future<void> _handleSubmit() async {
  setState(() => _isLoading = true);
  try {
    await authService.login(_email, _password);
    if (!mounted) return; // ← 在使用 context 之前進行保護
    context.go('/home');
  } on AuthException catch (e) {
    if (!mounted) return;
    ScaffoldMessenger.of(context).showSnackBar(SnackBar(content: Text(e.message)));
  } finally {
    if (mounted) setState(() => _isLoading = false);
  }
}

4. Widget 架構

提取為類別,而非方法

// 不好 — 返回 Widget 的私有方法,阻礙最佳化
Widget _buildHeader() {
  return Container(
    padding: const EdgeInsets.all(16),
    child: Text(title, style: Theme.of(context).textTheme.headlineMedium),
  );
}

// 好 — 獨立的 Widget 類別,允許 const、元素重用
class _PageHeader extends StatelessWidget {
  const _PageHeader(this.title);
  final String title;

  @override
  Widget build(BuildContext context) {
    return Container(
      padding: const EdgeInsets.all(16),
      child: Text(title, style: Theme.of(context).textTheme.headlineMedium),
    );
  }
}

const 傳播

// 不好 — 每次重建時都有新實例
child: Padding(
  padding: EdgeInsets.all(16.0),       // 不是 const
  child: Icon(Icons.home, size: 24.0), // 不是 const
)

// 好 — const 停止重建傳播
child: const Padding(
  padding: EdgeInsets.all(16.0),
  child: Icon(Icons.home, size: 24.0),
)

作用域重建

// 不好 — 每次計數器改變時整個頁面重建
class CounterPage extends ConsumerWidget {
  @override
  Widget build(BuildContext context, WidgetRef ref) {
    final count = ref.watch(counterProvider); // 重建所有東西
    return Scaffold(
      body: Column(children: [
        const ExpensiveHeader(), // 不必要地重建
        Text('$count'),
        const ExpensiveFooter(), // 不必要地重建
      ]),
    );
  }
}

// 好 — 隔離需要重建的部分
class CounterPage extends StatelessWidget {
  const CounterPage({super.key});

  @override
  Widget build(BuildContext context) {
    return const Scaffold(
      body: Column(children: [
        ExpensiveHeader(),        // 永不重建 (const)
        _CounterDisplay(),        // 僅此部分重建
        ExpensiveFooter(),        // 永不重建 (const)
      ]),
    );
  }
}

class _CounterDisplay extends ConsumerWidget {
  const _CounterDisplay();

  @override
  Widget build(BuildContext context, WidgetRef ref) {
    final count = ref.watch(counterProvider);
    return Text('$count');
  }
}

5. 狀態管理:BLoC/Cubit

// Cubit — 同步或簡單的非同步狀態
class AuthCubit extends Cubit<AuthState> {
  AuthCubit(this._authService) : super(const AuthState.initial());
  final AuthService _authService;

  Future<void> login(String email, String password) async {
    emit(const AuthState.loading());
    try {
      final user = await _authService.login(email, password);
      emit(AuthState.authenticated(user));
    } on AuthException catch (e) {
      emit(AuthState.error(e.message));
    }
  }

  void logout() {
    _authService.logout();
    emit(const AuthState.initial());
  }
}

// 在 Widget 中
BlocBuilder<AuthCubit, AuthState>(
  builder: (context, state) => switch (state) {
    AuthInitial() => const LoginForm(),
    AuthLoading() => const CircularProgressIndicator(),
    AuthAuthenticated(:final user) => HomePage(user: user),
    AuthError(:final message) => ErrorView(message: message),
  },
)

6. 狀態管理:Riverpod

// 自動銷毀的非同步 Provider
@riverpod
Future<List<Product>> products(Ref ref) async {
  final repo = ref.watch(productRepositoryProvider);
  return repo.getAll();
}

// 具有複雜變異的 Notifier
@riverpod
class CartNotifier extends _$CartNotifier {
  @override
  List<CartItem> build() => [];

  void add(Product product) {
    final existing = state.where((i) => i.productId == product.id).firstOrNull;
    if (existing != null) {
      state = [
        for (final item in state)
          if (item.productId == product.id) item.copyWith(quantity: item.quantity + 1)
          else item,
      ];
    } else {
      state = [...state, CartItem(productId: product.id, quantity: 1)];
    }
  }

  void remove(String productId) =>
      state = state.where((i) => i.productId != productId).toList();

  void clear() => state = [];
}

// 衍生 Provider (選擇器模式)
@riverpod
int cartCount(Ref ref) => ref.watch(cartNotifierProvider).length;

@riverpod
double cartTotal(Ref ref) {
  final cart = ref.watch(cartNotifierProvider);
  final products = ref.watch(productsProvider).valueOrNull ?? [];
  return cart.fold(0.0, (total, item) {
    // firstWhereOrNull (來自 collection 套件) 可避免產品缺失時的 StateError
    final product = products.firstWhereOrNull((p) => p.id == item.productId);
    return total + (product?.price ?? 0) * item.quantity;
  });
}

7. 使用 GoRouter 的導航

final router = GoRouter(
  initialLocation: '/',
  // refreshListenable 在身份驗證狀態改變時重新評估重導向
  refreshListenable: GoRouterRefreshStream(authCubit.stream),
  redirect: (context, state) {
    final isLoggedIn = context.read<AuthCubit>().state is AuthAuthenticated;
    final isGoingToLogin = state.matchedLocation == '/login';
    if (!isLoggedIn && !isGoingToLogin) return '/login';
    if (isLoggedIn && isGoingToLogin) return '/';
    return null;
  },
  routes: [
    GoRoute(path: '/login', builder: (_, __) => const LoginPage()),
    ShellRoute(
      builder: (context, state, child) => AppShell(child: child),
      routes: [
        GoRoute(path: '/', builder: (_, __) => const HomePage()),
        GoRoute(
          path: '/products/:id',
          builder: (context, state) =>
              ProductDetailPage(id: state.pathParameters['id']!),
        ),
      ],
    ),
  ],
);

8. 使用 Dio 的 HTTP

final dio = Dio(BaseOptions(
  baseUrl: const String.fromEnvironment('API_URL'),
  connectTimeout: const Duration(seconds: 10),
  receiveTimeout: const Duration(seconds: 30),
  headers: {'Content-Type': 'application/json'},
));

// 新增身份驗證攔截器
dio.interceptors.add(InterceptorsWrapper(
  onRequest: (options, handler) async {
    final token = await secureStorage.read(key: 'auth_token');
    if (token != null) options.headers['Authorization'] = 'Bearer $token';
    handler.next(options);
  },
  onError: (error, handler) async {
    // 防止無限重試循環:每個請求僅嘗試刷新一次
    final isRetry = error.requestOptions.extra['_isRetry'] == true;
    if (!isRetry && error.response?.statusCode == 401) {
      final refreshed = await attemptTokenRefresh();
      if (refreshed) {
        error.requestOptions.extra['_isRetry'] = true;
        return handler.resolve(await dio.fetch(error.requestOptions));
      }
    }
    handler.next(error);
  },
));

// 使用 Dio 的 Repository
class UserApiDataSource {
  const UserApiDataSource(this._dio);
  final Dio _dio;

  Future<User> getById(String id) async {
    final response = await _dio.get<Map<String, dynamic>>('/users/$id');
    return User.fromJson(response.data!);
  }
}

9. 錯誤處理架構

// 全域錯誤捕獲 — 在 main() 中設定
void main() {
  FlutterError.onError = (details) {
    FlutterError.presentError(details);
    crashlytics.recordFlutterFatalError(details);
  };

  PlatformDispatcher.instance.onError = (error, stack) {
    crashlytics.recordError(error, stack, fatal: true);
    return true;
  };

  runApp(const App());
}

// 用於生產環境的自訂 ErrorWidget
class App extends StatelessWidget {
  const App({super.key});

  @override
  Widget build(BuildContext context) {
    return MaterialApp.router(
      routerConfig: router,
      builder: (context, widget) {
        ErrorWidget.builder = (details) => const Scaffold(
          body: Center(
            child: Text('發生錯誤。請再試一次。'),
          ),
        );
        return widget!;
      },
    );
  }
}

10. 測試

BLoC/Cubit 單元測試

void main() {
  group('AuthCubit', () {
    late MockAuthService authService;
    late AuthCubit cubit;

    setUp(() {
      authService = MockAuthService();
      cubit = AuthCubit(authService);
    });

    tearDown(() => cubit.close());

    test('初始狀態是 AuthInitial', () {
      expect(cubit.state, const AuthInitial());
    });

    blocTest<AuthCubit, AuthState>(
      '登入成功時發射 [loading, authenticated]',
      build: () => cubit,
      act: (cubit) => cubit.login('email', 'password'),
      setUp: () => when(authService.login(any, any)).thenAnswer((_) async => user),
      expect: () => [
        const AuthLoading(),
        AuthAuthenticated(user),
      ],
    );
  });
}

Riverpod Widget 測試

void main() {
  testWidgets('CartTotal 正確計算', (tester) async {
    await tester.pumpWidget(
      ProviderScope(
        overrides: [
          cartNotifierProvider.overrideWith((ref) => _TestCartNotifier()),
          productsProvider.overrideWith((ref) => testProducts),
        ],
        child: const MaterialApp(home: CartTotalWidget()),
      ),
    );

    expect(find.text('\$150.00'), findsOneWidget);
  });
}

偏好使用 Fake 而非 Mock

// Fake — 提供真實行為的精簡版,更易維護
class FakeAuthService extends Fake implements AuthService {
  @override
  Future<User> login(String email, String password) async {
    if (email == '[email protected]' && password == 'password') {
      return User(id: '1', name: 'Test User');
    }
    throw AuthException('無效的憑證');
  }
}

關鍵原則: 始終偏好將狀態建模為密封類別或 Freezed 類別,以避免表示不可能狀態。在可重建的 Widget 樹中傳播 const。對於任何非同步操作,請檢查 mounted。在 Widget 層使用提供者進行反應式資料流。撰寫測試以驗證狀態轉換(BLoC)或提供者輸出(Riverpod)是否正確,而非僅測試實作細節。

Individual skills in this repo

This repo contains 20 individual skills — each has its own dedicated page.

accessibility

Design, implement, and audit inclusive digital products using WCAG 2.2 Level AA. Use when building or auditing UI that must meet WCAG 2.2 Level AA, or when reviewing a change for keyboard, contrast, or screen-reader support.

affaan-m/claude-api

Anthropic Claude API patterns for Python and TypeScript. Covers Messages API, streaming, tool use, vision, extended thinking, batches, prompt caching, and Claude Agent SDK. Use when building applications with the Claude API or Anthropic SDKs.

affaan-m/everything-claude-code

Development conventions and patterns for everything-claude-code. JavaScript project with conventional commits.

affaan-m/everything-claude-code

End-to-end marketing campaign planning and execution. Covers audience research, positioning, campaign angle definition, landing page copy, email sequences, social posts, ad copy, short-form video scripts, and content calendars. Use as the orchestration layer for multi-channel product launches. Use when planning or executing a multi-channel product launch, or producing landing page, email, social, or ad copy.

affaan-m/everything-claude-code-conventions

Development conventions and patterns for everything-claude-code. JavaScript project with conventional commits.

affaan-m/frontend-design

Create distinctive, production-grade frontend interfaces with high design quality. Use when the user asks to build web components, pages, or applications and the visual direction matters as much as the code quality.

affaan-m/gget

gget CLI and Python workflow for quick genomic database queries, sequence lookup, BLAST-style searches, enrichment checks, and reproducible bioinformatics evidence logs.

affaan-m/literature-review

Systematic literature-review workflow for academic, biomedical, technical, and scientific topics, including search planning, source screening, synthesis, citation checks, and evidence logging.

affaan-m/motion-ui

Production-ready UI motion system for React/Next.js. Use when implementing animations, transitions, or motion patterns.

affaan-m/project-guidelines-example

Example project-specific skill template based on a real production application.

affaan-m/pubmed-database

Direct PubMed and NCBI E-utilities search workflows for biomedical literature, MeSH queries, PMID lookup, citation retrieval, and API-backed literature monitoring.

affaan-m/scholar-evaluation

Structured scholarly-work evaluation for papers, proposals, literature reviews, methods sections, evidence quality, citation support, and research-writing feedback.

affaan-m/uspto-database

USPTO patent and trademark data workflow for official record lookup, PatentSearch queries, TSDR checks, assignment data, and reproducible IP research logs.

agent-architecture-audit

Full-stack diagnostic for agent and LLM applications. Audits the 12-layer agent stack for wrapper regression, memory pollution, tool discipline failures, hidden repair loops, and rendering corruption. Produces severity-ranked findings with code-first fixes. Essential for developers building agent applications, autonomous loops, or any LLM-powered feature. Use when an agent or LLM feature misbehaves and the failing layer is unknown, or before shipping an agent stack.

agent-eval

Head-to-head comparison of coding agents (Claude Code, Aider, Codex, etc.) on custom tasks with pass rate, cost, time, and consistency metrics. Use when choosing between coding agents, or when a change to an agent setup needs measured pass rate, cost, and time rather than an impression.

agent-harness-construction

Design and optimize AI agent action spaces, tool definitions, and observation formatting for higher completion rates. Use when defining or revising an agent

agentic-engineering

Operate as an agentic engineer using eval-first execution, decomposition, and cost-aware model routing. Use when planning or executing engineering work that agents will carry out end to end.

agentic-os

Build persistent multi-agent operating systems on Claude Code. Covers kernel architecture, specialist agents, slash commands, file-based memory, scheduled automation, and state management without external databases. Use when building a persistent multi-agent system on Claude Code with its own memory, commands, and scheduling.

agent-introspection-debugging

Structured self-debugging workflow for AI agent failures using capture, diagnosis, contained recovery, and introspection reports. Use when an agent run fails and you need a reproducible diagnosis instead of a retry.

agent-payment-x402

Add x402 payment execution to AI agents with per-task budgets, spending controls, and non-custodial wallets. Supports Base through agentwallet-sdk and X Layer through OKX Payments / OKX Agent Payments Protocol. Use when an agent must pay for something itself and needs per-task budgets, spending controls, and a non-custodial wallet.

相關技能