Skip to content

Commit e78db35

Browse files
committed
Element::build: Always rebuild, unless there is a cached build already
1 parent 9e31326 commit e78db35

File tree

2 files changed

+14
-11
lines changed

2 files changed

+14
-11
lines changed

lib/src/computed_flutter.dart

Lines changed: 12 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -6,20 +6,18 @@ class _Token {}
66

77
mixin _ComputedFlutterElementMixin on ComponentElement {
88
final _forceRebuild = ValueNotifier(_Token());
9-
var _dirty = false;
109
var _ignoreListener = true;
1110
ComputedSubscription<void>? _sub;
1211
Widget? _result;
1312
Object? _error;
1413
StackTrace? _trace;
15-
bool? _lastWasError;
14+
bool? _lastWasError; // If null: no cached result
1615

1716
@override
1817
Widget build() {
19-
if (_dirty) {
18+
if (_lastWasError == null) {
2019
_ignoreListener = true;
2120
_forceRebuild.value = _Token();
22-
_dirty = false;
2321
}
2422
_sub ??= Computed(() {
2523
_forceRebuild.react((p0) {}); // So that we can force rebuilds
@@ -36,10 +34,15 @@ mixin _ComputedFlutterElementMixin on ComponentElement {
3634
if (!_ignoreListener) super.markNeedsBuild();
3735
_ignoreListener = false;
3836
}, null);
39-
if (_lastWasError == true) {
40-
Error.throwWithStackTrace(_error!, _trace!);
41-
} else {
42-
return _result!;
37+
assert(_lastWasError != null);
38+
try {
39+
if (_lastWasError == true) {
40+
Error.throwWithStackTrace(_error!, _trace!);
41+
} else {
42+
return _result!;
43+
}
44+
} finally {
45+
_lastWasError = null; // Delete the cached result
4346
}
4447
}
4548

@@ -52,7 +55,7 @@ mixin _ComputedFlutterElementMixin on ComponentElement {
5255

5356
@override
5457
void markNeedsBuild() {
55-
_dirty = true;
58+
_lastWasError = null;
5659
super.markNeedsBuild();
5760
}
5861
}

test/widget_test.dart

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -183,13 +183,13 @@ void main() {
183183
await tester.pumpWidget(builder());
184184

185185
expect(find.byKey(v.value), findsOneWidget);
186-
expect(buildCnt[0], 2);
186+
expect(buildCnt[0], 4);
187187

188188
v.value = UniqueKey();
189189
await tester.pump();
190190

191191
expect(find.byKey(v.value), findsOneWidget);
192-
expect(buildCnt[0], 4);
192+
expect(buildCnt[0], 6);
193193
});
194194

195195
testWidgets('throwing computation throws during widget build',

0 commit comments

Comments
 (0)