shared_value 0.3.2 shared_value: ^0.3.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.
It's a low-boilerplate generalization of the Provider
state management solution.
Usage #
1. Initialize
main() {
// Insert Shared Value into the widget tree.
runApp(SharedValue.wrapApp(MyApp()));
}
// Create a `SharedValue` object that holds the value of our counter.
var counter = SharedValue(value: 0);
2. Use
// Use [counter] anywhere, even without a `BuildContext`
print(counter.value);
// Update [counter] anywhere.
counter.update((value) => value + 1);
// Rebuild [MyWidget] whenever [counter] changes.
class MyWidgetState extends State<MyWidget> {
@override
Widget build(BuildContext context) {
// The .of(context) bit makes this widget rebuild everytime counter is changed
int counterValue = counter.of(context);
return Text("Counter: $counterValue");
}
}
3. Persist
// provide a shared_prefences key
var counter = SharedValue(value: 0, key: "counter");
// Store [counter]'s value to shared preferences
await counter.store();
// Load [counter]'s value from shared preferences
await counter.load();
Efficiency #
Shared value is smart enough to only rebuild the widget that is using the shared value that was updated. No more, no less.