Learn/State Management/Provider Basics

Provider Basics

Use Provider for scalable state management

6 min~5 min read+35 FX

You can read this lesson publicly. Sign in to unlock exercises, quiz, and FX progress tracking.

Table of Contents

  1. 1. Provider Architecture Habit
  2. 2. This Lesson In Simple Words
  3. 3. Your level focus
  4. 4. One practical habit
  5. 5. Deep Understanding Guide
  6. 6. Why this lesson matters
  7. 7. Study flow for better retention
  8. 8. Quick Glossary

Having trouble? AI is here to help

Ask context-aware questions about this lesson. Free plan: 5 questions/day. Upgrade to Premium for higher daily limits.

Use the AI button at the bottom of the screen

Provider Package

Provider is the recommended way to manage app state in Flutter. It wraps InheritedWidget behind a simple API so that any widget in the tree can access shared state without manual prop drilling through constructors.

dart
1// 1. Create a state class with ChangeNotifier
2class CartProvider extends ChangeNotifier {
3 List<String> _items = [];
4
5 List<String> get items => _items;
6 int get itemCount => _items.length;
7
8 void addItem(String item) {
9 _items.add(item);
10 notifyListeners(); // Notify widgets to rebuild
11 }
12
13 void removeItem(String item) {
14 _items.remove(item);
15 notifyListeners();
16 }
17}
dart
1// 2. Provide it at the app level
2void main() {
3 runApp(
4 ChangeNotifierProvider(
5 create: (context) => CartProvider(),
6 child: MyApp(),
7 ),
8 );
9}
10
11// Or multiple providers
12runApp(
13 MultiProvider(
14 providers: [
15 ChangeNotifierProvider(create: (_) => CartProvider()),
16 ChangeNotifierProvider(create: (_) => AuthProvider()),
17 ],
18 child: MyApp(),
19 ),
20);
dart
1// 3. Consume the state in any widget
2class CartScreen extends StatelessWidget {
3 @override
4 Widget build(BuildContext context) {
5 // Read state
6 final cart = Provider.of<CartProvider>(context);
7
8 // Or use Consumer for targeted rebuilds
9 return Consumer<CartProvider>(
10 builder: (context, cart, child) {
11 return Text('Items: ${cart.itemCount}');
12 },
13 );
14 }
15}
16
17// Modify state
18ElevatedButton(
19 onPressed: () {
20 Provider.of<CartProvider>(context, listen: false)
21 .addItem('Product');
22 },
23 child: Text('Add to Cart'),
24)

listen: false

💡 listen: false

Use listen: false when you only need to call methods and do not need the widget to rebuild on state changes. This avoids unnecessary rebuilds and is more efficient.

Provider Architecture Habit

Keep provider classes focused on business state, not UI details.

  • provider handles data and rules
  • widgets handle presentation and user interaction

This separation keeps provider logic unit-testable and prevents UI coupling inside state classes.

Provider Performance Checklist

  • Use Consumer only around subtrees that depend on changing state.
  • Use listen false for action calls.
  • Avoid storing heavy derived values without need.
  • Split large providers when unrelated concerns grow together.

This Lesson In Simple Words

Use Provider for scalable state management. State is data that changes over time and controls what users see on the screen.

Your level focus

Connect each concept to real app features you can build.

One practical habit

Keep state as close as possible to where it is used, then lift it only when sharing is required.

Deep Understanding Guide

Why this lesson matters

State management is the difference between predictable apps and hard-to-debug UI behavior.

Study flow for better retention

  1. Map the lesson to one screen, one user action, and one expected output.
  2. Trace one user action from button tap to state update to rebuilt UI.
  3. If you can choose between two approaches and explain why, you are improving.

Common Mistakes To Avoid

  • Calling setState high in the tree when only a small part needs refresh.
  • Keeping duplicated state in multiple widgets without a single source of truth.
  • Mutating collections directly and expecting UI updates automatically.
  • Choosing a heavy state library before understanding simple local state.

Practice Sprint (15-25 Minutes)

  • Before running sample code, predict output and behavior in one sentence.
  • Build one feature with local setState, then refactor it to Provider or Riverpod.
  • Create loading, success, and error states for one async user flow.
  • Write a small state diagram for a login form and implement it.
  • Complete the required exercises without using hints on the first attempt.
  • Answer this question out loud before checking quiz options: "What method notifies widgets to rebuild in Provider?".

Quick Glossary

  • widget: A reusable UI building block in Flutter.
  • state: Data that can change and update what the user sees.
  • provider: A dependency and state sharing pattern in widget trees.
  • const: A compile-time constant that can reduce rebuild work.

Code Comprehension Drill

  • State the purpose of the code block in one line before reading details.
  • Mark inputs, state changes, and final output.
  • Change one small value and predict what should happen before running.
  • Run again and compare your prediction with actual behavior.

Lesson Completion Checklist

  • I can explain "Provider Basics" in simple words without reading notes.
  • I can describe one real app situation where this concept is required.
  • I know at least one mistake to avoid: Calling setState high in the tree when only a small part needs refresh.
  • I can modify one code sample and correctly predict the behavior change.
  • I can finish required exercises with understanding, not trial-and-error guesses.

Frequently Asked Questions

What method notifies widgets to rebuild in Provider?

ChangeNotifier.notifyListeners() tells all listening widgets to rebuild.

When should you use listen: false?

Use listen: false when you do not need the widget to rebuild on state changes.

What class do you extend to create a Provider state class?

ChangeNotifier provides notifyListeners() method to trigger rebuilds in listening widgets.

How do you provide multiple providers at once?

MultiProvider wraps multiple ChangeNotifierProvider instances for cleaner code.

Suggest Improvement

Something seems incorrect, unclear, or missing? Let us know.

About The Author

FlutterCraft editorial lessons are reviewed by experienced Flutter developers and updated to stay aligned with modern Flutter and Dart best practices.

Reference docs: Flutter Documentation, Dart Language Tour, and official package documentation where relevant.

Sign in to continue with exercises and quiz for this lesson.

Sign In to Continue