Sub topic 8
Refreshing Versus Reloading: Keeping Previous Data on Screen
This chapter assumes that you know Dart Future, async, await, and try/catch. You should also know Flutter's build method and basic Riverpod operations such as ref.watch and ref.read. The examples use the todosProvider AsyncNotifierProvider from the previous chapter.
An asynchronous provider can be in several loading situations. The first load has no data to display. A refresh or reload happens after the provider has already produced a result. In that case, replacing the screen with a full-page spinner makes the interface feel like it lost its data.
AsyncValue can retain the previous value while a new request runs. The UI can then keep showing the old list and add a smaller progress indicator.
The loading states
isLoading is true whenever an asynchronous operation is in progress. It does not tell you whether this is the first load or a later request.
Riverpod also provides isRefreshing and isReloading. These flags describe different kinds of later loads:
| Situation | Useful signal | Recommended display |
|---|---|---|
| First request, with no result yet | `isLoading` and no previous value | Full-page loading indicator |
| Explicit refresh, such as pull-to-refresh | `isRefreshing` | Keep the current data and show progress |
| Rebuild caused by a watched dependency | `isReloading` | Keep the current data and show progress |
The exact state also includes hasValue and hasError. A practical test for the first load is:
final isInitialLoad =
todos.isLoading && !todos.hasValue && !todos.hasError;Do not use isLoading alone to decide whether to hide the content. A provider can be loading while it still has a previous value.
Rendering data while a request runs
AsyncValue.when can skip its loading branch when a refresh or reload has previous state. Set both options explicitly when you want the old content to remain visible:
import 'package:flutter/material.dart';
import 'package:flutter_riverpod/flutter_riverpod.dart';
class TodoList extends ConsumerWidget {
const TodoList({super.key});
@override
Widget build(BuildContext context, WidgetRef ref) {
final todos = ref.watch(todosProvider);
final isInitialLoad =
todos.isLoading && !todos.hasValue && !todos.hasError;
final isUpdating = todos.isRefreshing || todos.isReloading;
if (isInitialLoad) {
return const Center(child: CircularProgressIndicator());
}
return Stack(
children: [
todos.when(
skipLoadingOnRefresh: true,
skipLoadingOnReload: true,
data: (items) => ListView(
children: [
for (final todo in items)
ListTile(title: Text(todo.title)),
],
),
error: (error, stackTrace) => Center(
child: Text('Could not load todos: $error'),
),
loading: () => const Center(
child: CircularProgressIndicator(),
),
),
if (isUpdating)
const Positioned(
top: 0,
left: 0,
right: 0,
child: LinearProgressIndicator(),
),
],
);
}
}During the initial request, the loading branch displays a centered spinner. During a refresh, skipLoadingOnRefresh lets when use the previous data branch instead. The list remains on screen, and the LinearProgressIndicator shows that new data is being requested.
The same approach works with Dart 3 pattern matching, but when makes the skip-loading behavior explicit.
Refreshing and reloading providers
Call ref.refresh when an event explicitly asks for fresh data. It immediately reruns the provider and returns the new result:
Future<void> refreshTodos(WidgetRef ref) async {
await ref.refresh(todosProvider.future);
}A pull-to-refresh callback can await this method:
RefreshIndicator(
onRefresh: () => refreshTodos(ref),
child: TodoList(),
)A reload can happen automatically when the notifier watches another provider in build:
@override
Future<List<Todo>> build() {
final filter = ref.watch(todoFilterProvider);
return ref
.watch(todoRepositoryProvider)
.fetchTodos(filter: filter);
}When todoFilterProvider changes, build runs again. Riverpod marks the asynchronous state as reloading. If the previous result is retained, the UI can continue displaying the old list while the filtered list loads.
Preserving state during manual requests
Automatic provider refreshes can retain previous state. A manual method that assigns const AsyncLoading() does not retain it:
state = const AsyncLoading();That assignment gives the UI a loading state without the old list. If you need to start a request inside the notifier, copy the previous state:
Future<void> reloadFromButton() async {
final previous = state;
state = const AsyncLoading<List<Todo>>().copyWithPrevious(previous);
state = await AsyncValue.guard(() {
return ref.read(todoRepositoryProvider).fetchTodos();
});
}AsyncValue.guard converts a successful result into AsyncData and catches a thrown exception as AsyncError. It handles the result of the request, while copyWithPrevious handles what the UI sees during the request.
Exercise
Why should a widget avoid using todos.isLoading as the only condition for showing a full-page spinner?
Show answer
isLoading is also true during refreshes and reloads. The provider may still have a previous value, so the widget should keep that value visible and show a smaller progress indicator instead.