Movatterモバイル変換


[0]ホーム

URL:


Skip to main content

docs.flutter.dev uses cookies from Google to deliver and enhance the quality of its services and to analyze traffic.

Learn more

Flutter 3.41 is live! Check out theFlutter 3.41 blog post!

Find widgets

How to use the Finder classes for testing widgets.

To locate widgets in a test environment, use theFinder classes. While it's possible to write your ownFinder classes, it's generally more convenient to locate widgets using the tools provided by theflutter_test package.

During aflutter run session on a widget test, you can also interactively tap parts of the screen for the Flutter tool to print the suggestedFinder.

This recipe looks at thefind constant provided by theflutter_test package, and demonstrates how to work with some of theFinders it provides. For a full list of available finders, see theCommonFinders documentation.

If you're unfamiliar with widget testing and the role ofFinder classes, review theIntroduction to widget testing recipe.

This recipe uses the following steps:

  1. Find aText widget.
  2. Find a widget with a specificKey.
  3. Find a specific widget instance.

1. Find aText widget

#

In testing, you often need to find widgets that contain specific text. This is exactly what thefind.text() method is for. It creates aFinder that searches for widgets that display a specificString of text.

dart
testWidgets('finds a Text widget',(tester)async{// Build an App with a Text widget that displays the letter 'H'.awaittester.pumpWidget(constMaterialApp(home:Scaffold(body:Text('H'))));// Find a widget that displays the letter 'H'.expect(find.text('H'),findsOneWidget);});

2. Find a widget with a specificKey

#

In some cases, you might want to find a widget based on the Key that has been provided to it. This can be handy if displaying multiple instances of the same widget. For example, aListView might display severalText widgets that contain the same text.

In this case, provide aKey to each widget in the list. This allows an app to uniquely identify a specific widget, making it easier to find the widget in the test environment.

dart
testWidgets('finds a widget using a Key',(tester)async{// Define the test key.consttestKey=Key('K');// Build a MaterialApp with the testKey.awaittester.pumpWidget(MaterialApp(key:testKey,home:Container()));// Find the MaterialApp widget using the testKey.expect(find.byKey(testKey),findsOneWidget);});

3. Find a specific widget instance

#

Finally, you might be interested in locating a specific instance of a widget. For example, this can be useful when creating widgets that take achild property and you want to ensure you're rendering thechild widget.

dart
testWidgets('finds a specific instance',(tester)async{constchildWidget=Padding(padding:EdgeInsets.zero);// Provide the childWidget to the Container.awaittester.pumpWidget(Container(child:childWidget));// Search for the childWidget in the tree and verify it exists.expect(find.byWidget(childWidget),findsOneWidget);});

Summary

#

Thefind constant provided by theflutter_test package provides several ways to locate widgets in the test environment. This recipe demonstrated three of these methods, and several more methods exist for different purposes.

If the above examples do not work for a particular use-case, see theCommonFinders documentation to review all available methods.

Complete example

#
dart
import'package:flutter/material.dart';import'package:flutter_test/flutter_test.dart';voidmain(){testWidgets('finds a Text widget',(tester)async{// Build an App with a Text widget that displays the letter 'H'.awaittester.pumpWidget(constMaterialApp(home:Scaffold(body:Text('H'))));// Find a widget that displays the letter 'H'.expect(find.text('H'),findsOneWidget);});testWidgets('finds a widget using a Key',(tester)async{// Define the test key.consttestKey=Key('K');// Build a MaterialApp with the testKey.awaittester.pumpWidget(MaterialApp(key:testKey,home:Container()));// Find the MaterialApp widget using the testKey.expect(find.byKey(testKey),findsOneWidget);});testWidgets('finds a specific instance',(tester)async{constchildWidget=Padding(padding:EdgeInsets.zero);// Provide the childWidget to the Container.awaittester.pumpWidget(Container(child:childWidget));// Search for the childWidget in the tree and verify it exists.expect(find.byWidget(childWidget),findsOneWidget);});}
Was this page's content helpful?

Unless stated otherwise, the documentation on this site reflects Flutter 3.38.6. Page last updated on 2025-09-22.View source orreport an issue.


[8]ページ先頭

©2009-2026 Movatter.jp