Skip to content

Commit 40ca315

Browse files
committed
Don't request new frame if not needed
0.3.3 README
1 parent 905822e commit 40ca315

File tree

4 files changed

+56
-28
lines changed

4 files changed

+56
-28
lines changed

README.md

Lines changed: 42 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -3,59 +3,78 @@
33
# Shared Value
44

55
A wrapper over [InheritedModel](https://api.flutter.dev/flutter/widgets/InheritedModel-class.html),
6-
this module allows users to easily share global state between multiple widgets.
6+
this module allows users to easily manage global state in your flutter apps.
77

8-
It's a low-boilerplate generalization of the `Provider` state management solution.
8+
At a high level, `SharedValue` puts your variables in an intelligent "container" that is flutter-aware.
9+
10+
It can be viewed as a low-boilerplate generalization of the `Provider` state management solution.
911

1012
## Usage
1113

1214
*1. Initialize*
1315

1416
```dart
17+
// This global SharedValue can be shared across the entire app
18+
final SharedValue<int> counter = SharedValue(
19+
value: 0, // initial value (optional)
20+
);
21+
1522
main() {
16-
// Insert Shared Value into the widget tree.
17-
runApp(SharedValue.wrapApp(MyApp()));
23+
runApp(
24+
// don't forget this!
25+
SharedValue.wrapApp(
26+
MyApp(),
27+
),
28+
);
1829
}
19-
20-
// Create a `SharedValue` object that holds the value of our counter.
21-
var counter = SharedValue(value: 0);
2230
```
2331

2432
*2. Use*
2533

34+
Unlike other state management solutions,
35+
SharedValue works everywhere you'd expect dart code to work, even without a `BuildContext`.
36+
2637
```dart
27-
// Use [counter] anywhere, even without a `BuildContext`
38+
// Read [counter]
2839
print(counter.value);
2940
30-
// Update [counter] anywhere.
31-
counter.update((value) => value + 1);
41+
// Update [counter]
42+
counter.value += 1;
3243
33-
// Rebuild [MyWidget] whenever [counter] changes.
34-
class MyWidgetState extends State<MyWidget> {
35-
@override
36-
Widget build(BuildContext context) {
44+
// Use [counter] in widgets, and let shared value do the rest.
45+
Widget build(BuildContext context) {
3746
38-
// The .of(context) bit makes this widget rebuild everytime counter is changed
39-
int counterValue = counter.of(context);
47+
// The .of(context) bit makes this widget rebuild automatically
48+
int counterValue = counter.of(context);
4049
41-
return Text("Counter: $counterValue");
42-
}
50+
return Text("Counter: $counterValue");
4351
}
4452
```
4553

4654
*3. Persist*
4755

4856
```dart
4957
// provide a shared_prefences key
50-
var counter = SharedValue(value: 0, key: "counter");
51-
52-
// Store [counter]'s value to shared preferences
53-
await counter.store();
58+
final SharedValue<int> counter = SharedValue(
59+
key: "counter", // disk storage key for shared_preferences (optional)
60+
autosave: true, // autosave to shared prefs when value changes (optional)
61+
);
5462
5563
// Load [counter]'s value from shared preferences
5664
await counter.load();
65+
66+
// Store [counter]'s value to shared preferences (enabling `autosave` does this automatically)
67+
await counter.store();
68+
```
69+
70+
*4. Be cool*
71+
72+
Use `.$` as an alias for `.value` :)
73+
74+
```
75+
print(counter.$);
5776
```
5877

5978
## Efficiency
6079

61-
Shared value is smart enough to only rebuild the widget that is using the shared value that was updated. No more, no less.
80+
Shared value is smart enough to only rebuild the widget that subscribes to updates using `.of(context)`, no more, no less.

example/pubspec.lock

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -127,7 +127,7 @@ packages:
127127
path: ".."
128128
relative: true
129129
source: path
130-
version: "0.3.1"
130+
version: "0.3.3"
131131
sky_engine:
132132
dependency: transitive
133133
description: flutter

lib/manager_widget.dart

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -23,9 +23,18 @@ class StateManagerWidget extends StatefulWidget {
2323
}
2424

2525
class StateManagerWidgetState extends State<StateManagerWidget> {
26-
Future<void> rebuild() async {
27-
await SchedulerBinding.instance.endOfFrame;
28-
if (mounted) setState(() {});
26+
Future<bool> rebuild() async {
27+
if (!mounted) return false;
28+
29+
// if there's a current frame,
30+
if (SchedulerBinding.instance.schedulerPhase != SchedulerPhase.idle) {
31+
// wait for the end of that frame.
32+
await SchedulerBinding.instance.endOfFrame;
33+
if (!mounted) return false;
34+
}
35+
36+
setState(() {});
37+
return true;
2938
}
3039

3140
@override

pubspec.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
name: shared_value
22
description: A straightforward way to manage global state in flutter apps.
3-
version: 0.3.2
3+
version: 0.3.3
44
author: Dev Aggarwal <[email protected]>
55
homepage: https://github.com/pycampers/flutter-global-state
66

0 commit comments

Comments
 (0)