Sub topic 9
Finding Widgets and Asserting on Them
This chapter assumes that you know basic Dart, can build simple Flutter widgets, and have used testWidgets with WidgetTester. You will learn how to locate widgets in the widget tree and check that they have the expected state.
Finders locate widgets
A finder is an object that searches the widget tree. Flutter provides several finders through the find object.
| Finder | What it searches for | Example |
|---|---|---|
| `find.text` | A `Text` widget with exact text | `find.text('Saved')` |
| `find.byType` | Widgets of a specific Dart type | `find.byType(TextField)` |
| `find.byIcon` | An icon with a specific icon data value | `find.byIcon(Icons.add)` |
| `find.byKey` | A widget with a specific key | `find.byKey(const Key('save'))` |
| `find.widgetWithText` | A widget containing specific text | `find.widgetWithText(ButtonStyleButton, 'Save')` |
A finder can match zero, one, or many widgets. The test should state how many matches are valid.
Matchers assert the result
A matcher describes an expected result. Flutter test matchers include:
findsOneWidget: exactly one widget matches.findsNothing: no widget matches.findsWidgets: one or more widgets match.findsNWidgets(2): exactly two widgets match.isTrue,isFalse, andequals(value): compare ordinary Dart values.
The following test checks both a visible label and a button identified by a key:
import 'package:flutter/material.dart';
import 'package:flutter_test/flutter_test.dart';
class SavePage extends StatelessWidget {
const SavePage({super.key});
@override
Widget build(BuildContext context) {
return Scaffold(
body: const Text('Draft'),
floatingActionButton: FloatingActionButton(
key: const Key('save-button'),
onPressed: () {},
child: const Icon(Icons.save),
),
);
}
}
void main() {
testWidgets('shows the draft and save button', (tester) async {
await tester.pumpWidget(
const MaterialApp(home: SavePage()),
);
expect(find.text('Draft'), findsOneWidget);
expect(find.byKey(const Key('save-button')), findsOneWidget);
expect(find.byIcon(Icons.save), findsOneWidget);
expect(find.text('Published'), findsNothing);
});
}The first argument to expect is the value being checked. The second argument is the matcher. A finder is itself a valid value for Flutter's finder matchers.
Choose stable finders
Text finders are useful when the text is part of the behavior a user must see. They can become fragile when wording changes or when localization adds another language.
Keys give a widget an explicit identity:
const Key('save-button')Use keys when a test must distinguish between similar widgets, such as rows in a list or multiple buttons with the same label. Do not add keys to every widget automatically. A key should communicate that the widget has a meaningful identity.
Prefer a finder that describes the behavior being tested. For example, find.text('Draft') checks visible content, while find.byKey(const Key('save-button')) checks that the save control exists.
Understand matching boundaries
Some widgets contain other widgets. find.text('Draft') may find a Text descendant inside a larger component. If several widgets contain the same text, the finder can match more than one widget and findsOneWidget will fail.
You can narrow a search with finder combinators:
final saveButton = find.ancestor(
of: find.text('Save'),
matching: find.byType(ElevatedButton),
);
expect(saveButton, findsOneWidget);The ancestor finder starts with the text and searches upward for a matching button. Other useful combinators include find.descendant and find.byWidgetPredicate, which allow more precise searches when standard finders are insufficient.
Exercise
Which finder and matcher should you use to verify that exactly one widget has the key login-button?
Show answer
Use expect(find.byKey(const Key('login-button')), findsOneWidget);.