shared_value 0.3.1 copy "shared_value: ^0.3.1" to clipboard
shared_value: ^0.3.1 copied to clipboard

outdated

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 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.

17
likes
0
pub points
93%
popularity

Publisher

unverified uploader

A straightforward way to manage global state in flutter apps.

Repository (GitHub)
View/report issues

License

unknown (license)

Dependencies

flutter, shared_preferences

More

Packages that depend on shared_value