Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .github/workflows/tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ jobs:
check:
strategy:
matrix:
flutter_version: [3.16.x, 3.13.x]
flutter_version: [3.29.x, 3.27.x]
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3
Expand Down
13 changes: 9 additions & 4 deletions lib/src/computed_flutter.dart
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,14 @@ import 'package:flutter/widgets.dart';
class _FlutterComputedImpl extends ComputedImpl<void> {
final ComponentElement _element;
_FlutterComputedImpl(this._element, void Function() build)
: super(build, false, false);
: super(build, false, false, true, null, null);

@override
void onDependencyUpdated() {
Set<Computed> onDependencyUpdated() {
// Delay until reeval() is called
_element.markNeedsBuild();
// we have no downstream
return {};
}

void reeval() {
Expand Down Expand Up @@ -46,8 +48,11 @@ mixin _ComputedFlutterElementMixin on ComponentElement {
_trace = s;
}
});
_sub ??= _c!.listen(null, null);
_c!.reeval();
if (_sub == null) {
_sub = _c!.listen(null, null);
} else {
_c!.reeval();
}
assert(_lastWasError != null);
try {
if (_lastWasError == true) {
Expand Down
6 changes: 3 additions & 3 deletions pubspec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -10,14 +10,14 @@ environment:
dependencies:
flutter:
sdk: flutter
computed: ^0.4.0
computed: ^0.5.1
#computed:
# path: ../computed
dev_dependencies:
flutter_test:
sdk: flutter
flutter_lints: ^3.0.0
test: ^1.24.3
flutter_lints: ^5.0.0
test: ^1.24.9

flutter:
uses-material-design: true
9 changes: 3 additions & 6 deletions test/computed_flutter_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -220,14 +220,11 @@ void main() {
expect(cCnt, 6);
expect(lCnt, 2);
expectation = 2;
c.fix(2);
expect(cCnt, 6);
expect(lCnt, 3);
expectation = null;
errExpectation = 3;
c.fixThrow(3);
expect(cCnt, 6);
expect(lCnt, 4);
controller.addError(3);
expect(cCnt, 7);
expect(lCnt, 3);
});
});
});
Expand Down
62 changes: 36 additions & 26 deletions test/widget_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ void main() {
await tester.pumpWidget(StatelessTestWidget(v, buildCnt));

expect(find.byKey(v.value), findsOneWidget);
expect(buildCnt[0], 2);
expect(buildCnt[0], 1);

var flag1 = false;

Expand All @@ -78,18 +78,18 @@ void main() {

expect(flag1, false, reason: 'No unexpected frames');
expect(find.byKey(v.value), findsOneWidget);
expect(buildCnt[0], 2);
expect(buildCnt[0], 1);

v.value = UniqueKey();
await tester.pump();

expect(find.byKey(v.value), findsOneWidget);
expect(buildCnt[0], 4);
expect(buildCnt[0], 2);

await tester.pump();

expect(find.byKey(v.value), findsOneWidget);
expect(buildCnt[0], 4);
expect(buildCnt[0], 2);
});

testWidgets('ComputedStatefulWidget', (tester) async {
Expand All @@ -101,39 +101,39 @@ void main() {

expect(find.byKey(v1.value), findsOneWidget);
expect(find.byKey(v2.value), findsOneWidget);
expect(buildCnt[0], 2);
expect(buildCnt[0], 1);

await tester.pump();

expect(find.byKey(v1.value), findsOneWidget);
expect(find.byKey(v2.value), findsOneWidget);
expect(buildCnt[0], 2);
expect(buildCnt[0], 1);

v1.value = UniqueKey();
await tester.pump();

expect(find.byKey(v1.value), findsOneWidget);
expect(find.byKey(v2.value), findsOneWidget);
expect(buildCnt[0], 4);
expect(buildCnt[0], 2);

await tester.pump();

expect(find.byKey(v1.value), findsOneWidget);
expect(find.byKey(v2.value), findsOneWidget);
expect(buildCnt[0], 4);
expect(buildCnt[0], 2);

v2.value = UniqueKey();
await tester.pump();

expect(find.byKey(v1.value), findsOneWidget);
expect(find.byKey(v2.value), findsOneWidget);
expect(buildCnt[0], 6);
expect(buildCnt[0], 3);

await tester.pump();

expect(find.byKey(v1.value), findsOneWidget);
expect(find.byKey(v2.value), findsOneWidget);
expect(buildCnt[0], 6);
expect(buildCnt[0], 3);
});

testWidgets('ComputedBuilder', (tester) async {
Expand All @@ -146,23 +146,23 @@ void main() {
}));

expect(find.byKey(v.value), findsOneWidget);
expect(buildCnt[0], 2);
expect(buildCnt[0], 1);

await tester.pump();

expect(find.byKey(v.value), findsOneWidget);
expect(buildCnt[0], 2);
expect(buildCnt[0], 1);

v.value = UniqueKey();
await tester.pump();

expect(find.byKey(v.value), findsOneWidget);
expect(buildCnt[0], 4);
expect(buildCnt[0], 2);

await tester.pump();

expect(find.byKey(v.value), findsOneWidget);
expect(buildCnt[0], 4);
expect(buildCnt[0], 2);
});

testWidgets('widgets are built at build() time', (tester) async {
Expand Down Expand Up @@ -198,27 +198,27 @@ void main() {
}));

expect(find.byKey(v.value), findsOneWidget);
expect(buildCnt[0], 2);
expect(buildCnt[1], 2);
expect(buildCnt[0], 1);
expect(buildCnt[1], 1);

await tester.pump();

expect(find.byKey(v.value), findsOneWidget);
expect(buildCnt[0], 2);
expect(buildCnt[1], 2);
expect(buildCnt[0], 1);
expect(buildCnt[1], 1);

v.value = UniqueKey();
await tester.pump();

expect(find.byKey(v.value), findsOneWidget);
expect(buildCnt[0], 4);
expect(buildCnt[1], 4);
expect(buildCnt[0], 2);
expect(buildCnt[1], 2);

await tester.pump();

expect(find.byKey(v.value), findsOneWidget);
expect(buildCnt[0], 4);
expect(buildCnt[1], 4);
expect(buildCnt[0], 2);
expect(buildCnt[1], 2);
});

testWidgets('swapping widgets on the same element works', (tester) async {
Expand All @@ -236,18 +236,18 @@ void main() {
await tester.pumpWidget(builder());

expect(find.byKey(v.value), findsOneWidget);
expect(buildCnt[0], 2);
expect(buildCnt[0], 1);

await tester.pumpWidget(builder());

expect(find.byKey(v.value), findsOneWidget);
expect(buildCnt[0], 4);
expect(buildCnt[0], 2);

v.value = UniqueKey();
await tester.pump();

expect(find.byKey(v.value), findsOneWidget);
expect(buildCnt[0], 6);
expect(buildCnt[0], 3);
});

testWidgets('throwing computation throws during widget build',
Expand Down Expand Up @@ -275,7 +275,7 @@ void main() {

FlutterError.onError = null;

expect(buildCnt[0], 2);
expect(buildCnt[0], 1);
expect(flag, true);
} finally {
FlutterError.onError = originalOnError;
Expand All @@ -297,4 +297,14 @@ void main() {

expect(controller.hasListener, false);
});

testWidgets('computed widgets can use async', (tester) async {
final key = UniqueKey();
await tester.pumpWidget(ComputedBuilder(builder: (ctx) {
Future.value().then((_) {});
return SizedBox.shrink(key: key);
}));

expect(find.byKey(key), findsOneWidget);
});
}