Nâng CaoFlutter iconFlutter

Làm thế nào để implement offline persistence với Riverpod?

Kết hợp AsyncNotifier với local storage (Hive, Isar, hoặc SharedPreferences). Pattern: khi build() chạy, đọc cache local trước rồi fetch API—nếu fetch thành công thì cập nhật cache. Dùng ref.invalidate() hoặc ref.refresh() để force refresh.

Ví dụ: @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; } }. Kết quả: app show dữ liệu cũ ngay lập tức, cập nhật khi có mạng.

Xem toàn bộ Flutter cùng filter theo level & chủ đề con.

Mở danh sách Flutter