Skip to content

Commit 027d17b

Browse files
Migrated testing/widget/introduction.md to null safety (#5863)
* Migrated testing/widget/introduction.md to null safety * new line breaks Co-authored-by: Miquel Beltran <miquelbeltran@users.noreply.github.com>
1 parent 3f2c0bd commit 027d17b

File tree

8 files changed

+257
-21
lines changed

8 files changed

+257
-21
lines changed
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
include: package:flutter_lints/flutter.yaml
2+
3+
linter:
4+
rules:
5+
avoid_print: false
6+
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
name: introduction
2+
description: A new Flutter project.
3+
4+
environment:
5+
sdk: '>=2.12.0 <3.0.0'
6+
7+
dependencies:
8+
flutter:
9+
sdk: flutter
10+
11+
dev_dependencies:
12+
flutter_test:
13+
sdk: flutter
14+
flutter_lints: ^1.0.2
15+
16+
flutter:
17+
uses-material-design: true
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
import 'package:flutter/material.dart';
2+
import 'package:flutter_test/flutter_test.dart';
3+
4+
// #docregion main
5+
void main() {
6+
// Define a test. The TestWidgets function also provides a WidgetTester
7+
// to work with. The WidgetTester allows you to build and interact
8+
// with widgets in the test environment.
9+
testWidgets('MyWidget has a title and message', (WidgetTester tester) async {
10+
// Test code goes here.
11+
});
12+
}
13+
// #enddocregion main
14+
15+
class MyWidget extends StatelessWidget {
16+
const MyWidget({
17+
Key? key,
18+
required this.title,
19+
required this.message,
20+
}) : super(key: key);
21+
22+
final String title;
23+
final String message;
24+
25+
@override
26+
Widget build(BuildContext context) {
27+
return MaterialApp(
28+
title: 'Flutter Demo',
29+
home: Scaffold(
30+
appBar: AppBar(
31+
title: Text(title),
32+
),
33+
body: Center(
34+
child: Text(message),
35+
),
36+
),
37+
);
38+
}
39+
}
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
import 'package:flutter/material.dart';
2+
import 'package:flutter_test/flutter_test.dart';
3+
4+
// #docregion main
5+
void main() {
6+
testWidgets('MyWidget has a title and message', (WidgetTester tester) async {
7+
// Create the widget by telling the tester to build it.
8+
await tester.pumpWidget(const MyWidget(title: 'T', message: 'M'));
9+
});
10+
}
11+
// #enddocregion main
12+
13+
class MyWidget extends StatelessWidget {
14+
const MyWidget({
15+
Key? key,
16+
required this.title,
17+
required this.message,
18+
}) : super(key: key);
19+
20+
final String title;
21+
final String message;
22+
23+
@override
24+
Widget build(BuildContext context) {
25+
return MaterialApp(
26+
title: 'Flutter Demo',
27+
home: Scaffold(
28+
appBar: AppBar(
29+
title: Text(title),
30+
),
31+
body: Center(
32+
child: Text(message),
33+
),
34+
),
35+
);
36+
}
37+
}
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
import 'package:flutter/material.dart';
2+
import 'package:flutter_test/flutter_test.dart';
3+
// ignore_for_file: unused_local_variable
4+
5+
// #docregion main
6+
void main() {
7+
testWidgets('MyWidget has a title and message', (WidgetTester tester) async {
8+
await tester.pumpWidget(const MyWidget(title: 'T', message: 'M'));
9+
10+
// Create the Finders.
11+
final titleFinder = find.text('T');
12+
final messageFinder = find.text('M');
13+
});
14+
}
15+
// #enddocregion main
16+
17+
class MyWidget extends StatelessWidget {
18+
const MyWidget({
19+
Key? key,
20+
required this.title,
21+
required this.message,
22+
}) : super(key: key);
23+
24+
final String title;
25+
final String message;
26+
27+
@override
28+
Widget build(BuildContext context) {
29+
return MaterialApp(
30+
title: 'Flutter Demo',
31+
home: Scaffold(
32+
appBar: AppBar(
33+
title: Text(title),
34+
),
35+
body: Center(
36+
child: Text(message),
37+
),
38+
),
39+
);
40+
}
41+
}
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
import 'package:flutter/material.dart';
2+
import 'package:flutter_test/flutter_test.dart';
3+
4+
// #docregion main
5+
void main() {
6+
testWidgets('MyWidget has a title and message', (WidgetTester tester) async {
7+
await tester.pumpWidget(const MyWidget(title: 'T', message: 'M'));
8+
final titleFinder = find.text('T');
9+
final messageFinder = find.text('M');
10+
11+
// Use the `findsOneWidget` matcher provided by flutter_test to verify
12+
// that the Text widgets appear exactly once in the widget tree.
13+
expect(titleFinder, findsOneWidget);
14+
expect(messageFinder, findsOneWidget);
15+
});
16+
}
17+
// #enddocregion main
18+
19+
class MyWidget extends StatelessWidget {
20+
const MyWidget({
21+
Key? key,
22+
required this.title,
23+
required this.message,
24+
}) : super(key: key);
25+
26+
final String title;
27+
final String message;
28+
29+
@override
30+
Widget build(BuildContext context) {
31+
return MaterialApp(
32+
title: 'Flutter Demo',
33+
home: Scaffold(
34+
appBar: AppBar(
35+
title: Text(title),
36+
),
37+
body: Center(
38+
child: Text(message),
39+
),
40+
),
41+
);
42+
}
43+
}
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
import 'package:flutter/material.dart';
2+
import 'package:flutter_test/flutter_test.dart';
3+
4+
void main() {
5+
// Define a test. The TestWidgets function also provides a WidgetTester
6+
// to work with. The WidgetTester allows building and interacting
7+
// with widgets in the test environment.
8+
testWidgets('MyWidget has a title and message', (WidgetTester tester) async {
9+
// Create the widget by telling the tester to build it.
10+
await tester.pumpWidget(const MyWidget(title: 'T', message: 'M'));
11+
12+
// Create the Finders.
13+
final titleFinder = find.text('T');
14+
final messageFinder = find.text('M');
15+
16+
// Use the `findsOneWidget` matcher provided by flutter_test to
17+
// verify that the Text widgets appear exactly once in the widget tree.
18+
expect(titleFinder, findsOneWidget);
19+
expect(messageFinder, findsOneWidget);
20+
});
21+
}
22+
23+
// #docregion widget
24+
class MyWidget extends StatelessWidget {
25+
const MyWidget({
26+
Key? key,
27+
required this.title,
28+
required this.message,
29+
}) : super(key: key);
30+
31+
final String title;
32+
final String message;
33+
34+
@override
35+
Widget build(BuildContext context) {
36+
return MaterialApp(
37+
title: 'Flutter Demo',
38+
home: Scaffold(
39+
appBar: AppBar(
40+
title: Text(title),
41+
),
42+
body: Center(
43+
child: Text(message),
44+
),
45+
),
46+
);
47+
}
48+
}
49+
// #enddocregion widget
50+

src/docs/cookbook/testing/widget/introduction.md

Lines changed: 24 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@ next:
1010
path: /docs/cookbook/testing/widget/finders
1111
---
1212

13+
<?code-excerpt path-base="../null_safety_examples/cookbook/testing/widget/introduction/"?>
14+
1315
{% assign api = site.api | append: '/flutter' -%}
1416

1517
In the [introduction to unit testing][] recipe,
@@ -59,18 +61,18 @@ dev_dependencies:
5961
Next, create a widget for testing. For this recipe,
6062
create a widget that displays a `title` and `message`.
6163

62-
<!-- skip -->
64+
<?code-excerpt "test/main_test.dart (widget)"?>
6365
```dart
6466
class MyWidget extends StatelessWidget {
65-
final String title;
66-
final String message;
67-
6867
const MyWidget({
69-
Key key,
70-
@required this.title,
71-
@required this.message,
68+
Key? key,
69+
required this.title,
70+
required this.message,
7271
}) : super(key: key);
7372
73+
final String title;
74+
final String message;
75+
7476
@override
7577
Widget build(BuildContext context) {
7678
return MaterialApp(
@@ -99,7 +101,7 @@ widget test and creates a `WidgetTester` to work with.
99101
This test verifies that `MyWidget` displays a given title and message.
100102
It is titled accordingly, and it will be populated in the next section.
101103

102-
<!-- skip -->
104+
<?code-excerpt "test/main_step3_test.dart (main)"?>
103105
```dart
104106
void main() {
105107
// Define a test. The TestWidgets function also provides a WidgetTester
@@ -120,12 +122,12 @@ The `pumpWidget` method builds and renders the provided widget.
120122
Create a `MyWidget` instance that displays "T" as the title
121123
and "M" as the message.
122124

123-
<!-- skip -->
125+
<?code-excerpt "test/main_step4_test.dart (main)"?>
124126
```dart
125127
void main() {
126128
testWidgets('MyWidget has a title and message', (WidgetTester tester) async {
127129
// Create the widget by telling the tester to build it.
128-
await tester.pumpWidget(MyWidget(title: 'T', message: 'M'));
130+
await tester.pumpWidget(const MyWidget(title: 'T', message: 'M'));
129131
});
130132
}
131133
```
@@ -176,11 +178,11 @@ Since you know you're looking for `Text` widgets, use the
176178
For more information about `Finder` classes, see the
177179
[Finding widgets in a widget test][] recipe.
178180

179-
<!-- skip -->
181+
<?code-excerpt "test/main_step5_test.dart (main)"?>
180182
```dart
181183
void main() {
182184
testWidgets('MyWidget has a title and message', (WidgetTester tester) async {
183-
await tester.pumpWidget(MyWidget(title: 'T', message: 'M'));
185+
await tester.pumpWidget(const MyWidget(title: 'T', message: 'M'));
184186
185187
// Create the Finders.
186188
final titleFinder = find.text('T');
@@ -200,11 +202,11 @@ value meets expectations.
200202
Ensure that the widgets appear on screen exactly one time.
201203
For this purpose, use the [`findsOneWidget`][] `Matcher`.
202204

203-
<!-- skip -->
205+
<?code-excerpt "test/main_step6_test.dart (main)"?>
204206
```dart
205207
void main() {
206208
testWidgets('MyWidget has a title and message', (WidgetTester tester) async {
207-
await tester.pumpWidget(MyWidget(title: 'T', message: 'M'));
209+
await tester.pumpWidget(const MyWidget(title: 'T', message: 'M'));
208210
final titleFinder = find.text('T');
209211
final messageFinder = find.text('M');
210212
@@ -235,6 +237,7 @@ matchers for common cases.
235237

236238
### Complete example
237239

240+
<?code-excerpt "test/main_test.dart"?>
238241
```dart
239242
import 'package:flutter/material.dart';
240243
import 'package:flutter_test/flutter_test.dart';
@@ -245,7 +248,7 @@ void main() {
245248
// with widgets in the test environment.
246249
testWidgets('MyWidget has a title and message', (WidgetTester tester) async {
247250
// Create the widget by telling the tester to build it.
248-
await tester.pumpWidget(MyWidget(title: 'T', message: 'M'));
251+
await tester.pumpWidget(const MyWidget(title: 'T', message: 'M'));
249252
250253
// Create the Finders.
251254
final titleFinder = find.text('T');
@@ -259,15 +262,15 @@ void main() {
259262
}
260263
261264
class MyWidget extends StatelessWidget {
262-
final String title;
263-
final String message;
264-
265265
const MyWidget({
266-
Key key,
267-
@required this.title,
268-
@required this.message,
266+
Key? key,
267+
required this.title,
268+
required this.message,
269269
}) : super(key: key);
270270
271+
final String title;
272+
final String message;
273+
271274
@override
272275
Widget build(BuildContext context) {
273276
return MaterialApp(

0 commit comments

Comments
 (0)