← Back to list

Teaching Flutter State Management? Start with Provider, Not Riverpod (Here’s Why and How)

A few weeks ago, I started teaching Flutter to a group of students. We were cruising through widgets, layouts, and navigation just fine…

Ishan Shrestha · 2025-07-17 10:15 · 100 claps · 3.9 min read
#flutter #flutter-app-development #state-management #provider #riverpod
Open on Medium ↗
Wiki topics: BIZ · Business Strategy EDU · Education & Learning 📱 · Mobile Development

Teaching Flutter State Management? Start with Provider, Not Riverpod (Here’s Why and How)

A few weeks ago, I started teaching Flutter to a group of students. We were cruising through widgets, layouts, and navigation just fine. But then came that moment every Flutter teacher dreads:

“Sir, I want to change a variable and update the UI. How do I do that?”

And just like that — we’d arrived at the land of state management.

I had a decision to make: Provider or Riverpod?

Now don’t get me wrong — I love Riverpod. It’s modern, scalable, and incredibly powerful. But for beginners, it’s like learning to drive in a Tesla with 100 buttons. What they really need first is a simple, sturdy bicycle.

So I chose Provider. And honestly? It was the best decision.

Let me walk you through how I introduced Provider in my class — in plain human-speak.

🧠 Wait, what is state management?

Imagine this: you tap a button in your app, and something on the screen changes. Maybe a number goes up. Maybe a list updates. That’s state changing, and your UI needs to know about it.

If you’ve been using setState() inside StatefulWidgets, you’ve already been doing it — just in the most basic way.

But as apps get more complex, setState() becomes messy. You end up with deeply nested widgets, spaghetti code, and hard-to-track bugs. That’s when you realize:

“I need something better.”

Enter: Provider.

🧺 What is Provider (and why is it beginner-friendly)?

Provider is like a data pipe. You put your data and logic in one place, and you connect widgets that care about that data. When the data changes, the widgets update automatically.

In fancy words: it’s a dependency injection and state management tool.

But in simple words: it lets your UI listen to your data.

Why is it great for teaching?

  • ✅ It’s officially recommended by the Flutter team.
  • ✅ It’s easy to understand.
  • ✅ It’s perfect for small to medium apps.
  • ✅ It sets the stage for learning more advanced tools later (like Riverpod or Bloc).

🔧 Step-by-step: Teaching Provider with a Counter App

I always start with a classic counter example — but without using setState. Here's how I break it down for my students.

Step 1: Add Provider to your project

Open terminal and run:

flutter pub add provider

Or you can add it to pubspec.yml Open pubspec.yml and add:

dependencies:
  flutter:
    sdk: flutter
  provider: ^6.1.0

Then run:

flutter pub get

Simple 👍.

Step 2: Create a CounterProviderclass

Create a file as counterProvider.dart

import 'package:flutter/material.dart';

class CounterProvider with ChangeNotifier {
  int _count = 0;

  int get count => _count;

  void increment() {
    _count++;
    notifyListeners(); // tell the UI to update
  }
}

What’s going on here? Let’s Break it down line by line

🎯 So why with ChangeNotifier?

ChangeNotifier is a built-in Flutter class that provides the notifyListeners() method.

🔐 int _count = 0;

This is a private variable that holds our current count value.

  • The underscore _ before count makes it private — meaning it can’t be accessed directly from outside this class.
  • Why private? To protect the data so it can only be changed in controlled ways (e.g., through the increment() method).

👀 int get count => _count;

This is a getter — a way to expose the private _count variable to the outside world (like the UI).

  • This lets widgets read the count value, but not change it directly.
  • It keeps the data flow one-way: from the provider to the UI.

👉 It’s like a peephole into the safe — you can see the number, but you can’t change it unless you use the right method.

🔊 notifyListeners()

  • It is the magic spell that refreshes the UI.

Step 3: Set up the Provider in main.dart

void main() {
  runApp(
    ChangeNotifierProvider(
      create: (_) => CounterProvider(),
      child: const MyApp(),
    ),
  );
}

I tell my students: this is like wrapping your app with a box of data. That data can now be accessed from anywhere inside the app.

Step 4: Use the Provider in the UI

Create a file CounterPage.dart

import 'package:flutter/material.dart';
import 'package:provider/provider.dart';
import 'counter_provider.dart';

class CounterPage extends StatelessWidget {
  const CounterPage({super.key});

  @override
  Widget build(BuildContext context) {
    final counter = Provider.of<CounterProvider>(context);

    return Scaffold(
      appBar: AppBar(title: const Text('Counter App')),
      body: Center(
        child: Text(
          'Count: ${counter.count}',
          style: const TextStyle(fontSize: 30),
        ),
      ),
      floatingActionButton: FloatingActionButton(
        onPressed: counter.increment,
        child: const Icon(Icons.add),
      ),
    );
  }
}

I usually ask: “Did you notice? We never used setState once.” And boom 💥 — the concept clicks.

🧠 What I Want My Students to Take Away

By the end of this example, they should understand:

  • ChangeNotifier holds the data and business logic.
  • notifyListeners() is how the UI knows to rebuild.
  • ChangeNotifierProvider provides the data to the widget tree.
  • Widgets can access the data using Provider.of().

If they get this, they’ve understood the core idea of reactive UI in Flutter.

🌱 What’s Next?

Once they’re comfortable, you can gradually introduce:

  • Consumer and Selector for more optimized builds
  • Using MultiProvider to manage multiple pieces of state
  • Real-world examples like login state, form validation, and API integration

Then, once they start asking “Can I make this cleaner?” or “Can I test this?” — that’s when I say:

“Alright, let’s talk about Riverpod.”

🧵 Final Thoughts

Teaching state management is tricky, but Provider makes it approachable.

It doesn’t overwhelm students with architecture patterns or jargon. It just gets the job done — clearly, cleanly, and with minimal setup.

If you’re teaching Flutter to beginners, don’t jump straight into the deep end. Start with Provider, let them swim confidently, and then show them the ocean of options out there.

P.S. Want a follow-up guide on real-world use cases like login/auth, theme switching, or API fetching with Provider? I’m working on that next. 😉


메타데이터
post_id
98aef60ef0da
slug
teaching-flutter-state-management-start-with-provider-not-riverpod-heres-why-and-how-98aef60ef0da
url
https://medium.com/@ishan941/teaching-flutter-state-management-start-with-provider-not-riverpod-heres-why-and-how-98aef60ef0da
canonical_url
https://medium.com/@ishan941/teaching-flutter-state-management-start-with-provider-not-riverpod-heres-why-and-how-98aef60ef0da
author_url
https://medium.com/@ishan941
status
ok
fetched_at
2026-06-25 07:00:49