shared_value 0.1.1

Flutter Android iOS web

A straightforward way to manage global state in flutter apps.

pub package

Shared Value #

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

It's a no-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) {

        // supply [counter] the `BuildContext` to rebuild widget
        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.

0
likes
100
pub points
55%
popularity

A straightforward way to manage global state in flutter apps.

Repository (GitHub)
View/report issues

Documentation

API reference

Uploader

devxpy@gmail.com

License

MIT (LICENSE)

Dependencies

flutter, shared_preferences

More

Packages that depend on shared_value