Kết hợp AsyncNotifier với local storage (Hive, Isar hoặc SharedPreferences): trong build(), đọc cache local trước rồi mới fetch API — fetch xong thì ghi lại cache.
dart
@riverpod
class PostsNotifier extends AsyncNotifier<List<Post>> {
Future<List<Post>> build() async {
final cached = await db.getPosts();
state = AsyncValue.data(cached);
final fresh = await api.getPosts();
await db.savePosts(fresh);
return fresh;
}
}App hiện data cũ ngay lập tức rồi tự cập nhật khi có mạng.
Dùng ref.invalidate() hoặc ref.refresh() khi cần ép làm mới.