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

A straightforward way to manage global state in flutter apps.

pub package

Shared Value #

A wrapper over InheritedModel, this module allows you to easily manage global state in flutter apps.

At a high level, SharedValue puts your variables in an intelligent "container" that is flutter-aware.

It can be viewed as a low-boilerplate generalization of the Provider state management solution.

Usage #

1. Initialize

// This global SharedValue can be shared across the entire app
final SharedValue<int> counter = SharedValue(
  value: 0, // initial value (optional)
);

main() {
  runApp(
    // don't forget this!
    SharedValue.wrapApp(
      MyApp(),
    ),
  );
}

2. Use

Unlike other state management solutions, SharedValue works everywhere you'd expect dart code to work, even without a BuildContext.

// Read [counter]
print(counter.value);

// Update [counter]
counter.value += 1;

// Use [counter] in widgets, and let shared value do the rest.
Widget build(BuildContext context) {

  // The .of(context) bit makes this widget rebuild automatically
  int counterValue = counter.of(context);

  return Text("Counter: $counterValue");
}

3. Persist

// provide a shared_prefences key
final SharedValue<int> counter = SharedValue(
  key: "counter", // disk storage key for shared_preferences (optional)
  autosave: true, // autosave to shared prefs when value changes (optional)
);

// Load [counter]'s value from shared preferences
await counter.load();

// Store [counter]'s value to shared preferences (enabling `autosave` does this automatically)
await counter.store();

4. Be cool

Use .$ as an alias for .value :)

print(counter.$);

Efficiency #

Shared value is smart enough to only rebuild the widget that subscribes to updates using .of(context), no more, no less.

17
likes
135
points
345
downloads

Publisher

unverified uploader

Weekly Downloads

A straightforward way to manage global state in flutter apps.

Homepage

Documentation

API reference

License

MIT (license)

Dependencies

flutter, shared_preferences

More

Packages that depend on shared_value