pub package

Shared Value

A wrapper over InheritedModel, this module allows users to easily share global state between multiple widgets.

Usage

  1. 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);
  1. 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);
        ...
    }
}
  1. Persist
/// Store [counter] value to shared preferences.
counter.load();

/// Load [counter] value from shared preferences.
counter.store();

Libraries

inherited_model
manager_widget
shared_value