In my State class I have declared a future:
Future<void> _testFuture;
and assign it in the initState like this:
super.initState();
_testFuture = Future(() async {
await Future.value(1); //can be any computation
});
and use it in the FutureBuilder like this:
FutureBuilder(
future: _testFuture,
builder: (context, snapshot) {
if (snapshot.connectionState == ConnectionState.done)
return Text('Hi');
else
return Center(
child: CircularProgressIndicator(),
);
},
),
This works fine when running the app normally with flutter run but when I try widget testing this using flutter test test/widget_test.dart:
void main() {
testWidgets('Testing', (WidgetTester tester) async {
// Build our app and trigger a frame.
await tester.runAsync(() async {
await tester.pumpWidget(MyApp());
await tester.pumpAndSettle();
});
}
it fails with :
══╡ EXCEPTION CAUGHT BY FLUTTER TEST FRAMEWORK ╞════════════════════════════════════════════════════
The following assertion was thrown while running async test code:
pumpAndSettle timed out
However, if I assign the future this way the same test passes with no issues:
super.initState();
Future<void> testFutures() async {
await Future.value(1);
}
_testFuture = testFutures();
What is the difference between the two ways of assigning the future ?

