Skip to content

Commit c7d1da3

Browse files
committed
better support for background tasks that fire state changes anytime, even when flutter in the process of building a frame
1 parent 72e6215 commit c7d1da3

File tree

4 files changed

+34
-15
lines changed

4 files changed

+34
-15
lines changed

example/lib/main.dart

Lines changed: 22 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
import 'dart:async';
2+
13
import 'package:flutter/material.dart';
24
import 'package:shared_value/shared_value.dart';
35

@@ -8,12 +10,19 @@ final SharedValue<int> counter = SharedValue(
810
autosave: true, // autosave to shared prefs when value changes
911
);
1012

13+
final SharedValue<Duration> randomValue = SharedValue();
14+
1115
Future<void> main() async {
1216
WidgetsFlutterBinding.ensureInitialized();
1317

1418
// load previous value from shared prefs
1519
counter.load();
1620

21+
DateTime startedAt = DateTime.now();
22+
Timer.periodic(Duration(milliseconds: 50), (timer) {
23+
randomValue.$ = DateTime.now().difference(startedAt);
24+
});
25+
1726
runApp(
1827
// don't forget this!
1928
SharedValue.wrapApp(
@@ -44,6 +53,10 @@ class MyApp extends StatelessWidget {
4453
'You have pushed the button this many times:',
4554
),
4655
CounterText(),
56+
Padding(
57+
padding: const EdgeInsets.all(32),
58+
child: RandomText(),
59+
),
4760
],
4861
),
4962
),
@@ -56,11 +69,11 @@ class MyApp extends StatelessWidget {
5669
class CounterText extends StatelessWidget {
5770
@override
5871
Widget build(BuildContext context) {
59-
print("CounterText.build()");
60-
6172
// The .of(context) bit makes this widget rebuild automatically
6273
int counterValue = counter.of(context);
6374

75+
print("CounterText.build() - $counterValue");
76+
6477
return Text(
6578
'$counterValue',
6679
style: Theme.of(context).textTheme.headline4,
@@ -85,3 +98,10 @@ class CounterButton extends StatelessWidget {
8598
counter.$ += 1;
8699
}
87100
}
101+
102+
class RandomText extends StatelessWidget {
103+
@override
104+
Widget build(BuildContext context) {
105+
return Text("Your time here: ${randomValue.of(context)}");
106+
}
107+
}

example/pubspec.lock

Lines changed: 2 additions & 2 deletions
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.0"
130+
version: "0.3.1"
131131
sky_engine:
132132
dependency: transitive
133133
description: flutter
@@ -190,5 +190,5 @@ packages:
190190
source: hosted
191191
version: "2.1.0-nullsafety.3"
192192
sdks:
193-
dart: ">=2.10.0-110 <2.11.0"
193+
dart: ">=2.10.0 <2.11.0"
194194
flutter: ">=1.12.13+hotfix.4 <2.0.0"

example/pubspec.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ description: A new Flutter project.
1414
version: 1.0.0+1
1515

1616
environment:
17-
sdk: ">=2.1.0 <3.0.0"
17+
sdk: ">=2.10.0 <3.0.0"
1818

1919
dependencies:
2020
flutter:

lib/manager_widget.dart

Lines changed: 9 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import 'package:flutter/scheduler.dart';
12
import 'package:flutter/widgets.dart';
23

34
import 'inherited_model.dart';
@@ -8,12 +9,11 @@ class StateManagerWidget extends StatefulWidget {
89
final StateManagerWidgetState state;
910
final Map<SharedValue, double> stateNonceMap;
1011

11-
const StateManagerWidget(
12-
this.child,
13-
this.state,
14-
this.stateNonceMap, {
15-
Key key,
16-
}) : super(key: key);
12+
const StateManagerWidget(this.child,
13+
this.state,
14+
this.stateNonceMap, {
15+
Key key,
16+
}) : super(key: key);
1717

1818
@override
1919
StateManagerWidgetState createState() {
@@ -22,10 +22,9 @@ class StateManagerWidget extends StatefulWidget {
2222
}
2323

2424
class StateManagerWidgetState extends State<StateManagerWidget> {
25-
void rebuild() {
26-
WidgetsBinding.instance.addPostFrameCallback((_) {
27-
if (mounted) setState(() {});
28-
});
25+
Future<void> rebuild() async {
26+
await SchedulerBinding.instance.endOfFrame;
27+
if (mounted) setState(() {});
2928
}
3029

3130
@override

0 commit comments

Comments
 (0)