text stringlengths 1 474 |
|---|
final titleFinder = find.text('T'); |
final messageFinder = find.text('M'); |
}); |
}<code_end> |
<topic_end> |
<topic_start> |
6. Verify the widget using a Matcher |
Finally, verify the title and message Text widgets appear on screen |
using the Matcher constants provided by flutter_test. |
Matcher classes are a core part of the test package, |
and provide a common way to verify a given |
value meets expectations.Ensure that the widgets appear on screen exactly one time. |
For this purpose, use the findsOneWidget Matcher. |
<code_start>void main() { |
testWidgets('MyWidget has a title and message', (tester) async { |
await tester.pumpWidget(const MyWidget(title: 'T', message: 'M')); |
final titleFinder = find.text('T'); |
final messageFinder = find.text('M'); |
// Use the `findsOneWidget` matcher provided by flutter_test to verify |
// that the Text widgets appear exactly once in the widget tree. |
expect(titleFinder, findsOneWidget); |
expect(messageFinder, findsOneWidget); |
}); |
}<code_end> |
<topic_end> |
<topic_start>Additional Matchers |
In addition to findsOneWidget, flutter_test provides additional |
matchers for common cases.<topic_end> |
<topic_start> |
Complete example |
<code_start>import 'package:flutter/material.dart'; |
import 'package:flutter_test/flutter_test.dart'; |
void main() { |
// Define a test. The TestWidgets function also provides a WidgetTester |
// to work with. The WidgetTester allows building and interacting |
// with widgets in the test environment. |
testWidgets('MyWidget has a title and message', (tester) async { |
// Create the widget by telling the tester to build it. |
await tester.pumpWidget(const MyWidget(title: 'T', message: 'M')); |
// Create the Finders. |
final titleFinder = find.text('T'); |
final messageFinder = find.text('M'); |
// Use the `findsOneWidget` matcher provided by flutter_test to |
// verify that the Text widgets appear exactly once in the widget tree. |
expect(titleFinder, findsOneWidget); |
expect(messageFinder, findsOneWidget); |
}); |
} |
class MyWidget extends StatelessWidget { |
const MyWidget({ |
super.key, |
required this.title, |
required this.message, |
}); |
final String title; |
final String message; |
@override |
Widget build(BuildContext context) { |
return MaterialApp( |
title: 'Flutter Demo', |
home: Scaffold( |
appBar: AppBar( |
title: Text(title), |
), |
body: Center( |
child: Text(message), |
), |
), |
); |
} |
}<code_end> |
<topic_end> |
<topic_start>Find widgets |
To locate widgets in a test environment, use the Finder |
classes. While it’s possible to write your own Finder classes, |
it’s generally more convenient to locate widgets using the tools |
provided by the flutter_test package.During a flutter run session on a widget test, you can also |
interactively tap parts of the screen for the Flutter tool to |
print the suggested Finder.This recipe looks at the find constant provided by |
the flutter_test package, and demonstrates how |
to work with some of the Finders it provides. |
For a full list of available finders, |
see the CommonFinders documentation.If you’re unfamiliar with widget testing and the role of |
Finder classes, |
review the Introduction to widget testing recipe.This recipe uses the following steps:<topic_end> |
<topic_start> |
1. Find a Text widget |
In testing, you often need to find widgets that contain specific text. |
This is exactly what the find.text() method is for. It creates a |
Finder that searches for widgets that display a specific String of text. |
<code_start>testWidgets('finds a Text widget', (tester) async { |
// Build an App with a Text widget that displays the letter 'H'. |
await tester.pumpWidget(const MaterialApp( |
home: Scaffold( |
body: Text('H'), |
), |
)); |
// Find a widget that displays the letter 'H'. |
expect(find.text('H'), findsOneWidget); |
});<code_end> |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.