Back to results for “UI Testing”
Flutter › Widget Testing in Flutter

How does the find object let you actually locate a specific given widget within the genuinely rendered widget tree during a widget test, such as by its own displayed text or its own specific widget type?

Beginner

Answer

The find object provides several genuinely convenient methods for actually locating a specific given widget within the currently rendered widget tree during a test, including find.text for locating a widget displaying a specific given piece of exact text, find.byType for locating a widget of a specific given particular class type, and find.byKey for locating a widget by its own explicitly assigned specific Key, and these particular locator methods return what is genuinely called a Finder, which you then properly pass into assertion functions like expect or into interaction methods like tap.
expect(find.byType(ElevatedButton), findsOneWidget);
expect(find.byKey(Key('submit-button')), findsOneWidget);
Real-world example A test verifying a login screen's own layout checks that exactly one single ElevatedButton widget and one specific widget with the key submit-button are both actually genuinely present somewhere within the currently rendered widget tree.

Common follow-ups: What genuinely does findsOneWidget actually specifically verify compared to the similar findsNothing or findsWidgets matchers?;When would you genuinely prefer using find.byKey over the considerably simpler find.text?