Loading...
Loading...
Persist Riverpod notifier state offline with Storage and persist(); riverpod_sqflite, JsonPersist, key, destroyKey, cache duration, testing with in-memory storage. Use when saving state across app restarts or offline. Use this skill when the user asks about offline persistence, persisting state, or Riverpod storage.
npx skill4agent add serverpod/skills-registry riverpod-offlinefinal storageProvider = FutureProvider<Storage<String, String>>((ref) async {
return JsonSqFliteStorage.open(
join(await getDatabasesPath(), 'riverpod.db'),
);
});buildref.watch(storageProvider.future)class TodoList extends AsyncNotifier<List<Todo>> {
Future<List<Todo>> build() async {
persist(
ref.watch(storageProvider.future),
key: 'todo_list',
encode: (todos) => todos.map((todo) => {'task': todo.task}).toList(),
decode: (json) => (json as List).map((todo) => Todo(task: todo['task'] as String)).toList(),
);
return fetchTodosFromServer();
}
}
()
class TodoList extends _$TodoList {
Future<List<Todo>> build() async {
persist(ref.watch(storageProvider.future));
return fetchTodosFromServer();
}
}persist(
ref.watch(storageProvider.future),
options: const StorageOptions(cacheTime: StorageCacheTime.unsafe_forever),
// ...
);options: const StorageOptions(destroyKey: '1.0'),await persist(ref.watch(storageProvider.future), key: 'todo_list', ...).future;
return state.value ?? <Todo>[];ProviderScope(
overrides: [
storageProvider.overrideWith((ref) => Storage<String, String>.inMemory()),
],
child: const MyApp(),
)