You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Riverpod should allow simple integration with offline storage solutions
A possibility is to have Riverpod serialize/deserialize the current state of a provider and send that to a database, associated with a unique key corresponding to the provider.
Reacted by mono — Masayuki Ono, Hamza Imran, Mhamed Lehbab, Nicolas Bourdin, Fuad Ar-Radhi, Sharad Chand, FredslundMagnus, Arnas, János Dobszai, Karol and 129 more
Reacted by Nico Essi, Hadi, Muhammed Rahif, Maxime ROBIN, Mattias Fjellvang, Joachim Bülow, Nabraj Khadka and Alex Kotliarsky
This sounds interesting. I might give this a try.
Do you already have an idea in mind? If so can you share some classes I should look into to be able to implement a draft for this functionality?
👋 thanks for offering to help, but this specific issue is in a bit of a unique position. I'd like to personally take care of this one
Reacted by Julian Bissekkou, mono — Masayuki Ono, Mr.monkey, tRuNKator, Žiga Povhe, Sailesh Dahal, myThorsten, Daniel Toubul, Muhammed Rahif, Mahesh Jamdade and 8 more
You mentioned this issue in #1168 and while the scope of this feature doesn't seem as narrow as just restoring the state after the OS kills your app, I thought you might be interested in this lib I recently made that restores the state of providers: https://github.com/North101/flutter_riverpod_restorable
Personally, I really like how states_rebuilder handles state persistence. This lets the user choose how offline data is stored while at the same time, abstracting away the actual storing of the data unless they want to. This solution would also help to mitigate any potential migration issues as the developer would be able to use their existing local storage provider.
Reacted by AmosLai, Andrew Kruglov and Mattias Fjellvang
I'm not concerned about updating data. Supporting modifying an existing provider should be straightforward.
My only concern is about deleting a provider.
When we delete a provider from the codebase, Riverpod has currently no way of knowing that it was removed.
From Riverpod's PoV, the provider could still be there and it just hasn't been used by the application yet.
So there's no way for Riverpod to do anything when a provider is deleted.
When using build_runner, we can solve that. Build_runner enables the generation of a list that contains all providers in the application. Then, Riverpod can compare that list with the DB's state and deduce what was removed.
But macros won't allow app-wide code-generation. So we won't be able to generate such a list.
@rrousselGit What if we add desired offline supported providers to a list something called offline support list on provider scope, then checking that list?
Is there any way to restart app when app was detached? After 15-20 min later or OS decided to exit the app, it gives us empty states and not even restarting app, I just tested with developer setting "don't keep activities" app starts give errors and its not getting normal state without exit the app and restart again, its huge issue, Is there any idea to handle this situation @rrousselGit ?
Dealing with migration is delaying this feature by way too much.
I've decided to:
Only include an "expiracy" Duration in Riverpod itself
push the responsibility of Data migration to whatever database you may use.
Let's ship something and move forward. We can revisit later if need be.
Reacted by Paul Grandperrin, Felix Brandtner, Burak Karahan, mori yuta, siamakabi, HPanda, Patt O'Brien, Andrey Gordeev, Maatteogekko, l1qu1d and 7 more
@MarlonJD this feels a bit off topic, so I'm not sure how to answer that.
For now I'm thinking about having a Notifier-only initial release, with the following API:
@Riverpod()
@JsonPersist() // < Extra generator, which generates added logic to use from/toJson.classModelNotifierextends_$ModelNotifier {
@overrideModelbuild() =>Model();
}
...
ProviderScope(
persist:SQLiteRiverpodPersist(<some parameters>),
)
I may add support for non-notifier providers later, but this feels more natural for an initial release.
The main value of notifiers is that we can override a bunch of methods in there, without making the API for this overly complex.
It also makes supporting non-code-generation simpler. For example:
Reacted by Burak Karahan, Nico Essi, Ryan Brereton, mono — Masayuki Ono, Shreeman Arjun Sahu, gitslav, Shiba Prasad Kar, M Atheer, re-ava and Alex Kotliarsky
Reacted by Arin Abdulmajeed, Lior Ageniho, Nabraj Khadka, re-ava, Alex Kotliarsky and bschulz87
Done in dev. We're getting close to a 3.0 release :)
The API changed slightly from the previously mentioned proposal. We instead have a few things:
Ref now has Ref.isFirstBuild. This gives Notifiers enough information to implement offline persistence on their own from scratch if they wanted to ; as that was the main missing piece.
If folks are unsatisfied with Riverpod's high-level API, this would enable them to write their own.
Notifiers can be persisted by mix-in Persistable, then using persist(...) in their build method:
classExampleextendsAsyncNotifier<User> withPersistable<User, KeyT, ValueT> {
@overrideFuture<User> build() async {
awaitpersist(
storage:/* An instance of Storage, an interface that enables Riverpod to communicate with a DB */
key:/* An instance of KeyT, as specified in the mixin. This is generally a string, such as "Example" here */
decode: (ValueT value) =>User.fromValue(value), /* your typical fromJson */
encode: (User user) => user.toValue(), /* your toJson */,
// optional:
options:StorageOptions(destroyKey: ..., cacheTime: ...),
);
}
}
Riverpod is agnostic from how objects are encoded/decoded. Although most people will likely use from/toJson, some may want protobuff or custom encoding protocols. Hence the generic KeyT/ValueT :)
When using code-gen, only storage needs to be specified:
@riverpod@JsonPersist() // We tell Riverpod to use from/toJson to encode the notifierclassExampleextends_$Example {
@overrideFuture<User> build() async {
awaitpersist(storage: ...);
}
}
Note that @JsonPersist is experimental. It currently does the bare minimum to support JSON.
It was either that or asking users to use two parts (like in Freezed) for JSON support. So Freezed users would end-up with:
I don't like that, so a limited JSON support was offered instead. We'll see what can be done about it in the future.
An AsyncValue.isFromCache
This new property gives information about whether the value was obtained from the database or not. It enables
the UI to differentiate persisted data from server data
A new riverpod_sqflite package is added.
This is an official SQFlite support in Riverpod. It offers a custom Storage subclass with JSON support.
The expected usage is:
final storageProvider =FutureProvider<JsonSqFliteStorage>((ref) async {
// Initialize SQFlite. We should share the Storage instance between providers.returnJsonSqFliteStorage.open(
join(awaitgetDatabasesPath(), 'riverpod.db'),
);
});
classExampleextendsAsyncNotifier<User> withPersistable<User, String, String> {
@overrideFuture<User> build() async {
awaitpersist(
storage: ref.watch(storageProvider.future),
// TODO pass other arguments
);
}
}
This is just a sneak peek. All of that will be explained properly in the documentation.
Reacted by Fabian Varela Bonett, Luca Venir, Patt O'Brien, HPanda, l1qu1d, Andrey Gordeev, mori yuta, mono — Masayuki Ono, Arin Abdulmajeed, Sáng Nguyen and 8 more
Reacted by gitslav, Burak Karahan, HPanda, Andrey Gordeev, mono — Masayuki Ono, Arin Abdulmajeed, Yamamuro Saeru, osaki, Andre Leite, Nico Essi and 4 more
Reacted by Luca Venir, Fabrizio Infante, HPanda, Andrey Gordeev, mono — Masayuki Ono, Chris Wickens, Yamamuro Saeru, Robin, Tobias B, Ozayr Moorad and 1 more
Reacted by Ahmed Elsayed, Thomas Ferreira, Paul Grandperrin, Fabian Varela Bonett, Hasan, Elijah Luckey, gitslav, Jakob Prossinger, HPanda, Andrey Gordeev and 8 more
Activity
JulianBissekkou commentedon Jan 11, 2022
This sounds interesting. I might give this a try.
Do you already have an idea in mind? If so can you share some classes I should look into to be able to implement a draft for this functionality?
rrousselGit commentedon Jan 11, 2022
👋 thanks for offering to help, but this specific issue is in a bit of a unique position. I'd like to personally take care of this one
huang12zheng commentedon Jan 23, 2022
I don't know if it's gonna be bad,

But I would say, you might want to take a look https://github.com/flutterdata/flutter_data
or
https://flutterdata.dev/docs/repositories/#architecture-overview
Finally, thank you for your library : )
(Translated above)
rrousselGit commentedon Jan 29, 2022
This feature is not really related to what you're suggesting
What this issue involves will most likely be two bits:
new parameters on providers for decoding/encoding:
a way to tell ProviderScope/ProviderContainer how to obtain the encoded value of a provider
ken-ng-esotec commentedon Mar 5, 2022
this sounds very interesting, is this being added to the roadmap? @rrousselGit
chimon2000 commentedon Mar 17, 2022
It is on the roadmap for https://github.com/rrousselGit/river_pod/milestone/1
North101 commentedon Mar 19, 2022
You mentioned this issue in #1168 and while the scope of this feature doesn't seem as narrow as just restoring the state after the OS kills your app, I thought you might be interested in this lib I recently made that restores the state of providers:
https://github.com/North101/flutter_riverpod_restorable
dJani97 commentedon Mar 20, 2022
@North101 the repo link does not work, so I'll leave a link to your package:
https://pub.dev/packages/flutter_riverpod_restorable
tim-smart commentedon Apr 1, 2022
I needed a solution for a generic riverpod persistence layer, and ended up with this:
https://github.com/tim-smart/riverpod_persistence/blob/main/test/riverpod_persistence_test.dart
Would be nice to have something official!
Reprevise commentedon Apr 9, 2022
Personally, I really like how states_rebuilder handles state persistence. This lets the user choose how offline data is stored while at the same time, abstracting away the actual storing of the data unless they want to. This solution would also help to mitigate any potential migration issues as the developer would be able to use their existing local storage provider.
rrousselGit commentedon Jul 31, 2022
That state_rebuild persistence wiki looks very similar to what I plan on doing for Riverpod. Thanks for sharing!
rrousselGit commentedon Aug 4, 2022
Supporting ChangeNotifier and the current StateNotifier may be difficult though.
I think I'll have to implement this feature only for the new StateNotifier syntax if that's OK.
54 remaining items
MarlonJD commentedon Jun 25, 2024
@rrousselGit Wouldn't it be enough if we give a warning in the debug console when there is a change in state controller that will change the database?
rrousselGit commentedon Jun 25, 2024
I'm not concerned about updating data. Supporting modifying an existing provider should be straightforward.
My only concern is about deleting a provider.
When we delete a provider from the codebase, Riverpod has currently no way of knowing that it was removed.
From Riverpod's PoV, the provider could still be there and it just hasn't been used by the application yet.
So there's no way for Riverpod to do anything when a provider is deleted.
rrousselGit commentedon Jun 25, 2024
When using build_runner, we can solve that. Build_runner enables the generation of a list that contains all providers in the application. Then, Riverpod can compare that list with the DB's state and deduce what was removed.
But macros won't allow app-wide code-generation. So we won't be able to generate such a list.
MarlonJD commentedon Jun 25, 2024
@rrousselGit What if we add desired offline supported providers to a list something called offline support list on provider scope, then checking that list?
rrousselGit commentedon Jun 26, 2024
It's tedious and error prone. You can easily forget to list one of such provider. And the linter wouldn't be able to help here.
masreplay commentedon Jul 14, 2024
Here is my fix
https://pub.dev/packages/riverpod_cache
MarlonJD commentedon Jul 19, 2024
Is there any way to restart app when app was detached? After 15-20 min later or OS decided to exit the app, it gives us empty states and not even restarting app, I just tested with developer setting "don't keep activities" app starts give errors and its not getting normal state without exit the app and restart again, its huge issue, Is there any idea to handle this situation @rrousselGit ?
rrousselGit commentedon Sep 21, 2024
I wonder if instead of:
We could have:
Although I don't like the split of the provider logic ; this could enable
ProviderScopeto automatically manage data migration.rrousselGit commentedon Sep 23, 2024
Dealing with migration is delaying this feature by way too much.
I've decided to:
Let's ship something and move forward. We can revisit later if need be.
MarlonJD commentedon Sep 23, 2024
Hey @rrousselGit, I think the same it's a right decision, btw what do you think about the #1032 (comment) this issue, how can we resolve this?
rrousselGit commentedon Sep 27, 2024
@MarlonJD this feels a bit off topic, so I'm not sure how to answer that.
For now I'm thinking about having a Notifier-only initial release, with the following API:
I may add support for non-notifier providers later, but this feels more natural for an initial release.
The main value of notifiers is that we can override a bunch of methods in there, without making the API for this overly complex.
It also makes supporting non-code-generation simpler. For example:
Shiba-Kar commentedon Nov 4, 2024
dam !! I was going to annotate
@JsonPersist()in m code base and call it a day ! ....Offline (#3968)
rrousselGit commentedon Feb 11, 2025
Done in
dev. We're getting close to a 3.0 release :)The API changed slightly from the previously mentioned proposal. We instead have a few things:
Refnow hasRef.isFirstBuild. This gives Notifiers enough information to implement offline persistence on their own from scratch if they wanted to ; as that was the main missing piece.If folks are unsatisfied with Riverpod's high-level API, this would enable them to write their own.
Notifiers can be persisted by mix-in
Persistable, then usingpersist(...)in theirbuildmethod:Riverpod is agnostic from how objects are encoded/decoded. Although most people will likely use from/toJson, some may want protobuff or custom encoding protocols. Hence the generic
KeyT/ValueT:)When using code-gen, only
storageneeds to be specified:Note that
@JsonPersistis experimental. It currently does the bare minimum to support JSON.It was either that or asking users to use two parts (like in Freezed) for JSON support. So Freezed users would end-up with:
I don't like that, so a limited JSON support was offered instead. We'll see what can be done about it in the future.
AsyncValue.isFromCacheThis new property gives information about whether the value was obtained from the database or not. It enables
the UI to differentiate persisted data from server data
riverpod_sqflitepackage is added.This is an official SQFlite support in Riverpod. It offers a custom
Storagesubclass with JSON support.The expected usage is:
This is just a sneak peek. All of that will be explained properly in the documentation.