Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/gabsn/riverpod_offline
Persist riverpod provider state using Hive
https://github.com/gabsn/riverpod_offline
Last synced: 21 days ago
JSON representation
Persist riverpod provider state using Hive
- Host: GitHub
- URL: https://github.com/gabsn/riverpod_offline
- Owner: gabsn
- Created: 2023-09-27T12:19:40.000Z (about 1 year ago)
- Default Branch: master
- Last Pushed: 2023-09-27T12:48:31.000Z (about 1 year ago)
- Last Synced: 2024-10-15T23:11:42.640Z (2 months ago)
- Language: Dart
- Size: 4.88 KB
- Stars: 0
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- Changelog: CHANGELOG.md
Awesome Lists containing this project
README
Persist riverpod state offline using Hive
## Features
✅ Persist state even when app is killed
✅ Debounce I/O disk operations to improve performances
❌ Support other stores than Hive## Getting started
Make sure to init Hive before using this package:
```dart
Hive.initFlutter();
```## Usage
1. Define a state class:
```dart
part 'counter_state.freezed.dart';
part 'counter_state.g.dart';@freezed
class CountersState with _$CountersState implements Serializable {
const factory CountersState({
@Default(0) int counter,
}) = _CountersState;factory CountersState.fromJson(Map json) =>
_$CountersStateFromJson(json);
}
```2. Write your provider:
```dart
part 'counter_provider.g.dart';@riverpod
class Counter extends _$Counter with PersistedState {
@override
String get boxName => 'Counter';@override
CountersState fromJson(Map json) =>
CountersState.fromJson(json);@override
CountersState build() {
// Load previous persisted state from disk
loadPersistedState();
return const CountersState();
}// Add any state mutations below
increment() {
state = state.copyWith(counter: state.counter + 1);
}
}
```3. Use the provider like you usually do:
```dart
// Rebuilds UI everytime counter changes
final state = ref.watch(counterProvider.select((s) => s.counter));
print(state.counter);
```If you don't want to use code generation, you can also use the following syntax:
```dart
class CounterNotifier extends PersistedNotifier {
CounterNotifier({
required super.boxName,
required super.defaultState,
required super.fromJson,
});// Add any state mutations below
increment() {
state = state.copyWith(counter: state.counter + 1);
}
}final counterProvider = NotifierProvider(
() => CounterNotifier(
boxName: 'Counter',
defaultState: const CounterState(length: 0),
fromJson: CounterState.fromJson,
),
);
```