New

🐦 Flutter Riverpod Tutorial for Beginners (Riverpod 3)

Build a Study Tasks app with Riverpod 3: ProviderScope, Notifier, derived providers, ref.watch vs ref.read, a FutureProvider with AsyncValue and a unit test.

Beginner ⏱️ ~40 minutes 🧰 Dart, Flutter, flutter_riverpod 3.5
← Flutter Track

What You'll Build in This Flutter Riverpod Tutorial

This Flutter Riverpod tutorial builds a small Study Tasks app: add tasks, tick them off, filter by All, Active or Done, see how many are left, and load a "study tip" from an async provider. Along the way you will use every core Riverpod 3 building block and write a unit test for your state without touching the UI.

The Provider topic in the Flutter track showed ChangeNotifier with the provider package. Riverpod is the next step many Flutter developers take: providers are plain global constants, they can depend on each other, and async loading and errors are built in. Google suggests "flutter riverpod tutorial", "flutter riverpod 3 tutorial" and "flutter riverpod tutorial for beginners" as people type, and this lesson uses the current 3.x API, not older 1.x or 2.x examples.

Prerequisites

  • Flutter installed and a new project: flutter create study_tasks. flutter_riverpod 3.5.0 needs Dart SDK 3.12 or newer.
  • Basic widgets, StatelessWidget, StatefulWidget and ListView.builder. The setState todo app tutorial covers them, and this lesson is the Riverpod version of a similar app.
  • Optional: the Flutter cheat sheet for quick syntax lookups.

Build the App with Riverpod Step by Step

1 Add the packages

From your project folder, add Riverpod. This lesson also uses the standalone material_ui package that replaced package:flutter/material.dart in Flutter 3.50 (see our material_ui migration guide). On an older Flutter, skip material_ui and import package:flutter/material.dart instead.

flutter pub add flutter_riverpod
flutter pub add material_ui

2 Wrap the app in ProviderScope

ProviderScope holds the state of every provider. Without it, reading a provider throws an error.

// lib/main.dart
import 'package:flutter_riverpod/flutter_riverpod.dart';
import 'package:material_ui/material_ui.dart';
import 'todo_screen.dart';

void main() {
  runApp(const ProviderScope(child: StudyApp()));
}

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

  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      title: 'Study Tasks',
      theme: ThemeData(colorSchemeSeed: Colors.indigo),
      home: const TodoScreen(),
    );
  }
}

3 Create an immutable model

Riverpod 3 notifies listeners only when the new state is not == to the old one, so always create new objects instead of changing old ones.

// lib/todo.dart
class Todo {
  const Todo({required this.id, required this.title, this.done = false});

  final String id;
  final String title;
  final bool done;

  Todo copyWith({bool? done}) =>
      Todo(id: id, title: title, done: done ?? this.done);
}

4 Hold the list in a Notifier

A Notifier owns a piece of state. build() returns the starting value, and your methods replace state. Every widget that watches the provider rebuilds when state changes.

// lib/providers.dart
import 'package:flutter_riverpod/flutter_riverpod.dart';
import 'todo.dart';

class TodoListNotifier extends Notifier<List<Todo>> {
  @override
  List<Todo> build() => const [];

  void add(String title) {
    final trimmed = title.trim();
    if (trimmed.isEmpty) return;
    state = [
      ...state,
      Todo(id: DateTime.now().microsecondsSinceEpoch.toString(), title: trimmed),
    ];
  }

  void toggle(String id) {
    state = [
      for (final todo in state)
        if (todo.id == id) todo.copyWith(done: !todo.done) else todo,
    ];
  }

  void remove(String id) {
    state = state.where((todo) => todo.id != id).toList();
  }
}

final todoListProvider =
    NotifierProvider<TodoListNotifier, List<Todo>>(TodoListNotifier.new);

5 Add a filter and derived providers

The filter is a tiny Notifier (Riverpod 3 treats StateProvider as legacy). Then two plain Providers derive values with ref.watch: they recompute automatically when the list or the filter changes. Add this to providers.dart:

enum TodoFilter { all, active, done }

class TodoFilterNotifier extends Notifier<TodoFilter> {
  @override
  TodoFilter build() => TodoFilter.all;

  void select(TodoFilter filter) => state = filter;
}

final todoFilterProvider =
    NotifierProvider<TodoFilterNotifier, TodoFilter>(TodoFilterNotifier.new);

final visibleTodosProvider = Provider<List<Todo>>((ref) {
  final todos = ref.watch(todoListProvider);
  return switch (ref.watch(todoFilterProvider)) {
    TodoFilter.all => todos,
    TodoFilter.active => todos.where((t) => !t.done).toList(),
    TodoFilter.done => todos.where((t) => t.done).toList(),
  };
});

final remainingCountProvider = Provider<int>((ref) {
  return ref.watch(todoListProvider).where((t) => !t.done).length;
});

6 Load async data with FutureProvider

A FutureProvider runs an async function once and caches the result. Here a short delay stands in for a network call. Add it to providers.dart too:

final studyTipProvider = FutureProvider<String>((ref) async {
  await Future.delayed(const Duration(seconds: 1));
  const tips = [
    'Study in 25-minute blocks with 5-minute breaks.',
    'Explain a topic out loud to check you really understand it.',
    'Review yesterday\'s notes before starting something new.',
  ];
  return tips[DateTime.now().day % tips.length];
});

7 Build the screen with ConsumerWidget

A ConsumerWidget gets a WidgetRef in build. Use ref.watch to read values (the widget rebuilds when they change), ref.read(...notifier) in callbacks to call methods, and ref.listen for one-off side effects such as a SnackBar.

// lib/todo_screen.dart
import 'package:flutter_riverpod/flutter_riverpod.dart';
import 'package:material_ui/material_ui.dart';
import 'providers.dart';

class TodoScreen extends ConsumerWidget {
  const TodoScreen({super.key});

  @override
  Widget build(BuildContext context, WidgetRef ref) {
    final todos = ref.watch(visibleTodosProvider);
    final remaining = ref.watch(remainingCountProvider);
    final filter = ref.watch(todoFilterProvider);

    ref.listen(remainingCountProvider, (previous, next) {
      if (previous != null && previous > 0 && next == 0) {
        ScaffoldMessenger.of(context).showSnackBar(
          const SnackBar(content: Text('All tasks done!')),
        );
      }
    });

    return Scaffold(
      appBar: AppBar(title: Text('Study Tasks ($remaining left)')),
      body: Column(
        children: [
          const StudyTipBanner(),
          const AddTodoField(),
          SegmentedButton<TodoFilter>(
            segments: const [
              ButtonSegment(value: TodoFilter.all, label: Text('All')),
              ButtonSegment(value: TodoFilter.active, label: Text('Active')),
              ButtonSegment(value: TodoFilter.done, label: Text('Done')),
            ],
            selected: {filter},
            onSelectionChanged: (selection) =>
                ref.read(todoFilterProvider.notifier).select(selection.first),
          ),
          Expanded(
            child: ListView.builder(
              itemCount: todos.length,
              itemBuilder: (context, index) {
                final todo = todos[index];
                return CheckboxListTile(
                  value: todo.done,
                  title: Text(todo.title),
                  onChanged: (_) =>
                      ref.read(todoListProvider.notifier).toggle(todo.id),
                  secondary: IconButton(
                    icon: const Icon(Icons.delete_outline),
                    tooltip: 'Delete',
                    onPressed: () =>
                        ref.read(todoListProvider.notifier).remove(todo.id),
                  ),
                );
              },
            ),
          ),
        ],
      ),
    );
  }
}

8 Handle loading and errors with AsyncValue

Watching a FutureProvider gives you an AsyncValue. In Riverpod 3 it is a sealed class, so a Dart switch covers data, error and loading. Add this widget to todo_screen.dart:

class StudyTipBanner extends ConsumerWidget {
  const StudyTipBanner({super.key});

  @override
  Widget build(BuildContext context, WidgetRef ref) {
    final tip = ref.watch(studyTipProvider);
    return Padding(
      padding: const EdgeInsets.all(12),
      child: switch (tip) {
        AsyncData(:final value) => Text('Tip: $value'),
        AsyncError(:final error) => Text('Could not load tip: $error'),
        _ => const LinearProgressIndicator(),
      },
    );
  }
}

If the future throws, Riverpod 3 retries it automatically a few times with a growing delay before showing the error, so a flaky network call often recovers on its own.

9 Keep local UI state in ConsumerStatefulWidget

A TextEditingController belongs to one widget, not to app state, so keep it in a ConsumerStatefulWidget. Its State has a ref property you can use anywhere:

class AddTodoField extends ConsumerStatefulWidget {
  const AddTodoField({super.key});

  @override
  ConsumerState<AddTodoField> createState() => _AddTodoFieldState();
}

class _AddTodoFieldState extends ConsumerState<AddTodoField> {
  final _controller = TextEditingController();

  @override
  void dispose() {
    _controller.dispose();
    super.dispose();
  }

  void _submit() {
    ref.read(todoListProvider.notifier).add(_controller.text);
    _controller.clear();
  }

  @override
  Widget build(BuildContext context) {
    return Padding(
      padding: const EdgeInsets.symmetric(horizontal: 12),
      child: TextField(
        controller: _controller,
        decoration: InputDecoration(
          labelText: 'New task',
          suffixIcon: IconButton(
            icon: const Icon(Icons.add),
            tooltip: 'Add task',
            onPressed: _submit,
          ),
        ),
        onSubmitted: (_) => _submit(),
      ),
    );
  }
}

Run the app with flutter run. Add a few tasks, tick them off and switch filters: the counter in the app bar and the list update without a single setState.

10 Test your state without the UI

Because providers do not need a BuildContext, you can test them with a ProviderContainer. Riverpod 3 adds ProviderContainer.test(), which disposes the container automatically when the test ends.

// test/todo_test.dart
import 'package:flutter_riverpod/flutter_riverpod.dart';
import 'package:flutter_test/flutter_test.dart';
import 'package:study_tasks/providers.dart';

void main() {
  test('adding and toggling a task updates the count', () {
    final container = ProviderContainer.test();
    final notifier = container.read(todoListProvider.notifier);

    notifier.add('Revise Dart records');
    expect(container.read(remainingCountProvider), 1);

    final id = container.read(todoListProvider).first.id;
    notifier.toggle(id);
    expect(container.read(remainingCountProvider), 0);
  });
}

Run it with flutter test.

How Riverpod Works: Key Ideas

Practice Tasks

  1. Add a "Clear done" button in the app bar that removes every completed task with one new TodoListNotifier method.
  2. Turn studyTipProvider into an AsyncNotifierProvider with a refresh() method, and add a refresh button to the banner.
  3. Write a second test that checks visibleTodosProvider returns only finished tasks when the filter is TodoFilter.done.

Flutter Riverpod Quiz

Five quick questions on this tutorial.

Frequently Asked Questions

What is Riverpod in Flutter?

Riverpod is a state management and caching package for Flutter and Dart. You declare providers as global constants, wrap the app in ProviderScope, and read them from widgets with ref.watch. It does not depend on BuildContext, so providers are easy to combine and test.

Is Riverpod better than Provider?

Both are made by the same author. Provider is simpler and ties state to the widget tree; Riverpod catches more mistakes at compile time, lets providers depend on each other with ref.watch, and handles async data with AsyncValue. Riverpod is a common choice for new apps, but Provider still works.

What changed in Riverpod 3.0?

Riverpod 3 unified Ref (no more typed Ref subclasses), moved StateProvider, StateNotifierProvider and ChangeNotifierProvider to a legacy import, retries failing providers automatically, pauses listeners in widgets that are not visible, and uses == to decide whether to notify listeners.

When should I use ref.watch and ref.read?

Use ref.watch inside build methods and providers so the UI or provider updates when the value changes. Use ref.read inside callbacks such as onPressed, usually with .notifier to call a method on a Notifier. Do not call ref.read in build to avoid missed updates.

Is StateProvider removed in Riverpod 3?

Not removed, but it is now considered legacy. Import it from package:flutter_riverpod/legacy.dart. For new code, the Riverpod docs recommend a small Notifier class instead, as this tutorial does for the filter.

Sources

Checked November 25, 2026 against flutter_riverpod 3.5.0 (published November 6, 2026) and the Riverpod 3 documentation.

What to Try Next

Keep learning Flutter

Head back to the Flutter track for the full learning path and more hands-on projects.

Back to the Flutter Track