shared_value 0.0.2 shared_value: ^0.0.2 copied to clipboard
A straightforward way to manage global state in flutter apps.
Shared Value #
A wrapper over InheritedModel, this module allows users to easily share global state between multiple widgets.
Usage #
- Initialize
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) {
return value + 1;
});
/// Rebuild [MyWidget] whenever [counter] changes.
class MyWidgetState extends State<MyWidget> {
@override
Widget build(BuildContext context) {
counterValue = counter.of(context);
...
}
}
- Persist
/// Store [counter] value to shared preferences.
counter.load();
/// Load [counter] value from shared preferences.
counter.store();