Sub topic 4
Asserting with `findsOneWidget`, `findsNothing`, and Friends
Assume you know basic Dart, Flutter widgets, testWidgets, WidgetTester, and finders. A finder selects widgets from the test widget tree. A matcher checks whether the finder selected the expected number of widgets.
Choosing a Finder Matcher
Use the matcher that describes the exact UI state your test requires.
| Matcher | Passes when the finder matches |
|---|---|
| `findsOneWidget` | Exactly one widget |
| `findsNothing` | Zero widgets |
| `findsNWidgets(n)` | Exactly `n` widgets |
| `findsWidgets` | One or more widgets |
findsOneWidget is useful for unique controls, such as a page title or a save button. findsNothing checks that an old message, loading indicator, or removed screen is absent. findsNWidgets checks an exact count. findsWidgets is useful when several matches are valid, but it does not verify the exact number.
A test should assert both what appears and what disappears after an interaction.
A Worked Example
This test builds a counter, verifies its initial value, taps the button, and checks the rebuilt output:
import 'package:flutter/material.dart';
import 'package:flutter_test/flutter_test.dart';
class Counter extends StatefulWidget {
const Counter({super.key});
@override
State<Counter> createState() => _CounterState();
}
class _CounterState extends State<Counter> {
int _count = 0;
@override
Widget build(BuildContext context) {
return Scaffold(
body: Column(
children: [
const Text('Counter'),
Text('$_count'),
],
),
floatingActionButton: FloatingActionButton(
onPressed: () => setState(() => _count++),
child: const Icon(Icons.add),
),
);
}
}
void main() {
testWidgets('shows one title and updates the count', (tester) async {
await tester.pumpWidget(const MaterialApp(home: Counter()));
expect(find.text('Counter'), findsOneWidget);
expect(find.text('0'), findsOneWidget);
expect(find.byIcon(Icons.add), findsOneWidget);
expect(find.text('1'), findsNothing);
await tester.tap(find.byIcon(Icons.add));
await tester.pump();
expect(find.text('0'), findsNothing);
expect(find.text('1'), findsOneWidget);
});
}The first expect evaluates the finder immediately. After tap, the state changes, but Flutter needs a new frame to rebuild the tree. pump creates that frame. The second group of assertions then checks the new tree.
Exact Counts and Duplicate Matches
A finder can match more widgets than you expect. For example, find.text('Save') may find a page button and a menu item. findsOneWidget exposes that ambiguity instead of allowing the test to tap an unintended widget.
Use a more precise finder when necessary:
expect(
find.widgetWithText(ElevatedButton, 'Save'),
findsOneWidget,
);Use findsNWidgets(3) when three matching rows are part of the contract. Use findsWidgets when the number may vary but at least one result must exist.
How the Assertion Fits Together
Exercise
Which matcher should you use to verify that a loading spinner has disappeared?
Show answer
Use expect(find.byType(CircularProgressIndicator), findsNothing);.