Hydrated provides a Subject that automatically persists to Flutter's local storage and hydrates on creation!
All values are persisted with shared_preferences and restored with automatic hydration.
final count$ = HydratedSubject<int>("count", seedValue: 0);
/// count$ will automagically be hydrated with 42 next time it is created
count$.add(42);
class HydratedBloc {
final _count$ = HydratedSubject<int>("count", seedValue: 0);
ValueObservable<int> get count$ => _count$.stream;
Sink<int> get setCount => _count$.sink;
dispose() {
_count$.close();
}
}
We support all shared_preferences types.
final count$ = HydratedSubject<int>("count");
We also support serialized classes with hydrate and persist arguments.
final user$ = HydratedSubject<User>(
"user",
hydrate: (String s) => User.fromJSON(s),
persist: (User user) => user.toJSON(),
);
Hydrated is mock tested with all supported types and is dogfooded by its creator.
Run this command:
With Flutter:
$ flutter pub add hydrated
This will add a line like this to your package's pubspec.yaml (and run an implicit dart pub get):
dependencies:
hydrated: ^2.0.1
Alternatively, your editor might support flutter pub get. Check the docs for your editor to learn more.
Now in your Dart code, you can use:
import 'package:hydrated/hydrated.dart';
import 'package:flutter/material.dart';
import 'package:hydrated/hydrated.dart';
void main() => runApp(MyApp());
class MyApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
return MaterialApp(
title: 'Hydrated Demo',
theme: ThemeData(
primarySwatch: Colors.blue,
),
home: MyHomePage(title: 'Hydrated Demo'),
);
}
}
class MyHomePage extends StatelessWidget {
final String _title;
final _count = HydratedSubject<int>("count", seedValue: 0);
MyHomePage({
Key? key,
required String title,
}) : _title = title,
super(key: key);
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text(this._title),
),
body: Center(
child: StreamBuilder<int>(
stream: _count,
initialData: _count.value,
builder: (context, snap) => Column(
mainAxisAlignment: MainAxisAlignment.center,
children: [
Text('You have pushed the button this many times:'),
Text(
'${snap.data}',
style: Theme.of(context).textTheme.headline4,
),
],
),
),
),
floatingActionButton: FloatingActionButton(
onPressed: _incrementCounter,
tooltip: 'Increment',
child: Icon(Icons.add),
),
);
}
void _incrementCounter() {
_count.value++;
}
void dispose() {
_count.close();
}
}
hydrated was originally developed by @lukepighetti.