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 ChangeNotifier models, providing them to the widget tree, consuming state with Consumer or Provider.of, or optimizing rebuilds.
한국어 요약은 준비 중입니다. 원문 설명을 표시하고 있습니다.
설치 방법 보기설치하기 전에 에이전트에게 전달되는 지시 내용을 확인할 수 있습니다.
This skill defines how to correctly use ChangeNotifier with the provider package for state management in Flutter.
Extend ChangeNotifier to manage state. Keep internal state private and expose unmodifiable views. Call notifyListeners() on every state change.
class CartModel extends ChangeNotifier {
final List<Item> _items = [];
UnmodifiableListView<Item> get items => UnmodifiableListView(_items);
void add(Item item) {
_items.add(item);
notifyListeners();
}
void removeAll() {
_items.clear();
notifyListeners();
}
}
ChangeNotifierProvider(
create: (context) => CartModel(),
child: MyApp(),
)
ChangeNotifierProvider automatically disposes of the model when it is no longer needed.MultiProvider when you need to provide multiple models:MultiProvider(
providers: [
ChangeNotifierProvider(create: (_) => CartModel()),
ChangeNotifierProvider(create: (_) => UserModel()),
],
child: MyApp(),
)
Consumer<CartModel>(
builder: (context, cart, child) => Stack(
children: [
if (child != null) child,
Text('Total price: ${cart.totalPrice}'),
],
),
child: const SomeExpensiveWidget(), // rebuilt only once
)
Consumer<CartModel>, not Consumer).child parameter to pass widgets that don't depend on the model — they are built once and reused.Consumer widgets as deep in the widget tree as possible to minimize the scope of rebuilds:HumongousWidget(
child: AnotherMonstrousWidget(
child: Consumer<CartModel>(
builder: (context, cart, child) {
return Text('Total price: ${cart.totalPrice}');
},
),
),
)
Use Provider.of<T>(context, listen: false) when you only need to call methods on the model, not react to state changes:
Provider.of<CartModel>(context, listen: false).removeAll();
Consumer if only a small part depends on the model.listen: false in callbacks (e.g., onPressed) where you trigger actions but don't need rebuilds.Write unit tests for your ChangeNotifier models to verify state changes and notifications:
test('adding item updates total', () {
final cart = CartModel();
var notified = false;
cart.addListener(() => notified = true);
cart.add(Item('Book'));
expect(cart.items.length, 1);
expect(notified, isTrue);
});
아직 리뷰가 없습니다. 사용해 본 소감을 남겨 주세요.
로그인 없이 쓸 수 있습니다. 사용 후기, 질문, 작동하지 않은 환경 등을 편하게 남겨 주세요.
아직 댓글이 없습니다. 첫 댓글을 남겨 보세요.
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.
한국어 요약은 준비 중입니다. 원문 설명을 표시하고 있습니다.