Sub topic 4
Declaring an AsyncNotifier and Its Provider
Assume you know Dart Future, async, and await, Flutter widget basics, and basic Riverpod APIs such as Provider, ref.watch, and ref.read. This chapter focuses on the declaration that connects asynchronous application logic to Riverpod.
What an AsyncNotifier provides
An AsyncNotifier is a Riverpod notifier whose state may be loading, successful, or failed. Its state type is still the type of the successful value. For example, an AsyncNotifier<List<Todo>> eventually provides a list of todos, but Riverpod also tracks whether that list is loading or whether loading failed.
You declare the notifier as a Dart class and then expose it through an AsyncNotifierProvider.
The provider has two important generic type arguments:
| Type | Meaning |
|---|---|
| `TodosNotifier` | The notifier class that owns the logic |
| `List<Todo>` | The successful data type exposed by the provider |
A widget that watches todosProvider receives an AsyncValue<List<Todo>>, rather than a bare List<Todo>.
Declare the notifier and provider
The following example defines a repository dependency, a notifier, and its provider.
import 'package:flutter_riverpod/flutter_riverpod.dart';
class Todo {
const Todo(this.id, this.title);
final String id;
final String title;
}
abstract class TodoRepository {
Future<List<Todo>> fetchTodos();
}
final todoRepositoryProvider = Provider<TodoRepository>((ref) {
throw UnimplementedError('Override with a real repository');
});
class TodosNotifier extends AsyncNotifier<List<Todo>> {
@override
Future<List<Todo>> build() {
final repository = ref.watch(todoRepositoryProvider);
return repository.fetchTodos();
}
}
final todosProvider =
AsyncNotifierProvider<TodosNotifier, List<Todo>>(TodosNotifier.new);TodosNotifier extends AsyncNotifier<List<Todo>>. This tells Riverpod that the notifier owns a value whose successful form is List<Todo>.
The build method is required. Riverpod calls it when the provider needs its initial state. Because this method returns a Future<List<Todo>>, Riverpod automatically represents the operation as an AsyncValue<List<Todo>>.
The ref property belongs to the notifier. Calling ref.watch(todoRepositoryProvider) reads the repository and makes it a dependency of this notifier. The repository can be a production implementation in the application and a fake implementation in tests.
TodosNotifier.new is a constructor tear-off. It passes the notifier's default constructor to AsyncNotifierProvider. You could write a named constructor instead, but the tear-off is the usual concise form.
Access the notifier and its state
A provider exposes both state and notifier access.
Use ref.watch(todosProvider) when a widget needs to rebuild as the asynchronous state changes:
class TodoList extends ConsumerWidget {
const TodoList({super.key});
@override
Widget build(BuildContext context, WidgetRef ref) {
final state = ref.watch(todosProvider);
return state.when(
loading: () => const CircularProgressIndicator(),
error: (error, stackTrace) => Text('Could not load todos: $error'),
data: (todos) => Text('${todos.length} todos'),
);
}
}Use ref.read(todosProvider.notifier) when calling a method on the notifier. read does not subscribe the caller to state changes. This distinction matters for buttons and event handlers, where you need to perform an action rather than rebuild when the state changes.
For example:
FilledButton(
onPressed: () {
ref.read(todosProvider.notifier);
},
child: const Text('Use notifier'),
)At this point, the notifier only loads its initial data. Later methods can change its state, such as adding a todo or retrying a request. Those methods belong in TodosNotifier, while widgets remain responsible for displaying the current AsyncValue.
Exercise
What does AsyncNotifierProvider<TodosNotifier, List<Todo>> mean, and what type does a widget receive from ref.watch(todosProvider)?
Show answer
It connects the TodosNotifier class to the provider, declares List<Todo> as the successful data type, and gives a watching widget an AsyncValue<List<Todo>>.