shared_value 0.0.1 shared_value: ^0.0.1 copied to clipboard
A straightforward way to manage global state in flutter apps.
Shared Value #
A wrapper over flutter's InheritedModel, shared value allows users to easily share a global state value between multiple widgets.
Usage #
- initiate
main() {
/// Insert Shared Value into the widget tree.
runApp(SharedValue.wrapApp(MyApp()));
}
/// Create a Shared Value object that holds the value of a counter.
/// ("counter" is used as key for shared_preferences).
var counter = SharedValue("counter", value: 0);
- use
/// Use `counter` anywhere.
print(counter.value);
/// Muatate `counter` anywhere.
counter.mutate((value) {
value += 1;
});
/// Rebuild widgets whenever `counter` changes.
class MyWidgetState extends State<MyWidget> {
@override
Widget build(BuildContext context) {
counterValue = counter.of(context);
...
}
}
- persist
/// Store counter to shared preferences.
counter.load();
/// Load counter's value from shared preferences.
counter.store();