Last updated:
0 purchases
hydrated state notifier
Hydrated State Notifier #
Features #
An extension to the state_notifier
library which automatically persists and restores states using HydratedStorage. A hive implementation of HydratedStorage is hydrated_state_notifier_hive.
Usage #
Setup HydratedStorage #
Install
Add package to your project with
dart pub add hydrated_state_notifier hydrated_state_notifier_hive
copied to clipboard
Import package
Add below lines in your dart file
import 'package:hydrated_state_notifier/hydrated_state_notifier.dart';
import 'package:hydrated_state_notifier_hive/hydrated_state_notifier_hive.dart';
copied to clipboard
Initialize
void main() async {
WidgetsFlutterBinding.ensureInitialized();
/// Initialize the common storage by providing [HiveHydratedStorage].
/// You can also provide your own implementation of [HydratedStorage].
HydratedStorage.storage = await HiveHydratedStorage.build(
storageDirectory: kIsWeb
? ''
: await getTemporaryDirectory(),
);
runApp(App())
}
copied to clipboard
Create a HydratedStateNotifier #
Does automatic state persistence for the [StateNotifier] class.
class CounterController extends HydratedStateNotifier<int> {
CounterController() : super(0);
void increment() => state = state + 1;
@override
int fromJson(Map<String, dynamic> json) => json['value'] as int;
@override
Map<String, int> toJson(int state) => { 'value': state };
}
copied to clipboard
Now the CounterController will automatically persist/restore their state. We
can increment the counter value, hot restart, kill the app, etc... and the
previous state will be retained.
It uses the same cache for the same type. Use the id parameter if you intend
to have different cache for different intance of same type.
class CounterController extends HydratedStateNotifier<int> {
CounterController(String id) : super(0, id: id);
void increment() => state = state + 1;
@override
int fromJson(Map<String, dynamic> json) => json['value'] as int;
@override
Map<String, int> toJson(int state) => { 'value': state };
}
CounterController('first_counter');
CounterController('second_counter');
copied to clipboard
Create a HydratedStateController #
A subclass of [HydratedStateNotifier] for simple scenarios.
final counterController = HydratedStateController(
0,
fromJson: (json) => json['count'] as int,
toJson: (state) => {'count': state},
);
copied to clipboard
Now the counterController will automatically persist/restore their state. We
can increment the counter value, hot restart, kill the app, etc... and the
previous state will be retained.
It uses the same cache for the same type. Use the id parameter if you intend
to have different cache for different intance of same type.
final counterController = HydratedStateController(
0,
fromJson: (json) => json['count'] as int,
toJson: (state) => {'count': state},
id: 'counter_1',
);
copied to clipboard
final counterController = HydratedStateController(
0,
fromJson: (json) => json['count'] as int,
toJson: (state) => {'count': state},
id: 'counter_2',
);
copied to clipboard
HydratedMixin #
class CounterController extends StateNotifier<int> with HydratedMixin {
CounterController() : super(0) {
// Hydrate must be called if using HydratedMixin or implementing any
// [HydratedStateNotifier], or [HydratedStateController].
hydrate();
}
void increment() => state = state + 1;
@override
int fromJson(Map<String, dynamic> json) => json['value'] as int;
@override
Map<String, int> toJson(int state) => { 'value': state };
@override
String get id => '';
@override
// Use a common storage or pass another
HydratedStorage get storage => HydratedStorage.storage;
@override
// Will be used for writing migrations in a later version
int get version => 1;
}
copied to clipboard
HydratedStorage #
You can implement a custom Storage by simply implementing the HydratedStorage interface and initializing HydratedStateNotifier with the custom Storage.
// my_hydrated_storage.dart
class MyHydratedStorage implements HydratedStorage {
@override
Object? read(String key) {
// TODO: implement read
}
@override
Future<void> write(String key, Object? value) async {
// TODO: implement write
}
@override
Future<void> delete(String key) async {
// TODO: implement delete
}
@override
Future<void> clear() async {
// TODO: implement clear
}
}
copied to clipboard
Setting a common storage for all.
// main.dart
HydratedBloc.storage = MyHydratedStorage();
runApp(MyApp());
copied to clipboard
or providing storage through the parameters:
final counterController = HydratedStateController(
0,
fromJson: (json) => json['count'] as int,
toJson: (state) => {'count': state},
storage: MyHydratedStorage(),
);
// or
class CounterController extends HydratedStateNotifier<int> {
CounterController(String id) : super(0, id: id, storage: MyHydratedStorage());
void increment() => state = state + 1;
@override
int fromJson(Map<String, dynamic> json) => json['value'] as int;
@override
Map<String, int> toJson(int state) => { 'value': state };
}
copied to clipboard
Additional information #
This is based on hydrated_bloc, and
hydrated_notifier.
For personal and professional use. You cannot resell or redistribute these repositories in their original state.
There are no reviews.