Use when working on accessibility, a11y, WCAG, ARIA, screen readers, keyboard nav, focus order, contrast, alt text, captions, reduced motion, or target sizes; not language/culture/device (see inclusive-design).
中文简介正在准备中。当前显示原文说明。
Use when setting up providers, consuming state, optimizing rebuilds, using ProxyProvider, or migrating from deprecated providers (Provider package).
中文简介正在准备中。当前显示原文说明。
查看安装方法安装前可以查看交给 AI 代理的指令内容。
This skill defines how to correctly use the provider package in Flutter applications.
| Provider | Use for |
|---|---|
Provider | Exposing any immutable value |
ChangeNotifierProvider | Mutable state with ChangeNotifier |
FutureProvider | Exposing a Future result |
StreamProvider | Exposing a Stream |
ProxyProvider / ChangeNotifierProxyProvider | Objects that depend on other providers |
MultiProvider(
providers: [
Provider<Something>(create: (_) => Something()),
ChangeNotifierProvider(create: (_) => MyNotifier()),
FutureProvider<String>(create: (_) => fetchData(), initialData: ''),
],
child: MyApp(),
)
MultiProvider to group multiple providers and avoid deeply nested trees.ChangeNotifierProvider automatically disposes the model when it is no longer needed.ProxyProvider instead.StackOverflowError.Always specify the generic type for type safety:
// Listen and rebuild on change — only valid inside build() or a Provider's update method
final count = context.watch<MyModel>().count;
// Access without listening — use in callbacks, not inside build()
context.read<MyModel>().increment();
// Listen to only part of the state
final count = context.select<MyModel, int>((m) => m.count);
Use Consumer<T> or Selector<T, R> widgets when you need fine-grained rebuilds and cannot access a descendant BuildContext:
Consumer<MyModel>(
builder: (context, model, child) => Text('${model.count}'),
child: const ExpensiveWidget(), // rebuilt only once
)
Selector<MyModel, int>(
selector: (_, model) => model.count,
builder: (_, count, __) => Text('$count'),
)
Use ProxyProvider or ChangeNotifierProxyProvider for objects that depend on other providers or values that can change:
MultiProvider(
providers: [
Provider<Auth>(create: (_) => Auth()),
ProxyProvider<Auth, Api>(
update: (_, auth, __) => Api(auth.token),
),
],
)
initState or constructors — use them in build, callbacks, or lifecycle methods where the widget is fully mounted.ChangeNotifier; use Provider.value() with a StatefulWidget if needed.Implement toString or DiagnosticableTreeMixin to improve how your objects appear in Flutter DevTools:
class MyModel with DiagnosticableTreeMixin {
final int count;
MyModel(this.count);
@override
String toString() => 'MyModel(count: $count)';
}
ValueListenableProvider is deprecated. Use Provider with ValueListenableBuilder instead:
ValueListenableBuilder<int>(
valueListenable: myValueListenable,
builder: (context, value, _) {
return Provider<int>.value(
value: value,
child: MyApp(),
);
},
)
还没有评价。欢迎分享你的使用感受。
无需登录即可发表。欢迎分享使用感受、提问,或说明在哪些环境下无法运行。
还没有评论。来写第一条吧。
Use when working on accessibility, a11y, WCAG, ARIA, screen readers, keyboard nav, focus order, contrast, alt text, captions, reduced motion, or target sizes; not language/culture/device (see inclusive-design).
中文简介正在准备中。当前显示原文说明。
Use when creating a feature, designing folder structure, adding repositories/services/view models, wiring dependency injection, or deciding which layer owns logic.
中文简介正在准备中。当前显示原文说明。
Use when creating a Cubit or Bloc, modeling state with sealed classes or status enums, wiring BlocBuilder/BlocListener/BlocProvider, writing bloc tests, or choosing between Cubit and Bloc.
中文简介正在准备中。当前显示原文说明。
Use when asked to review a PR, MR, branch, or diff, audit changed files, or check code quality.
中文简介正在准备中。当前显示原文说明。
Use when writing switch statements, refactoring if-else chains, creating data classes, choosing records vs classes, destructuring values, or modernizing pre-Dart-3 code.
中文简介正在准备中。当前显示原文说明。
Use when building AI agents in Dart, implementing Genkit flows or tools, integrating LLMs into Dart or Flutter applications, or using Genkit Dart plugins.
中文简介正在准备中。当前显示原文说明。