Sub topic 7
Reading Widget Properties and State
Assume you know basic Dart, Flutter widgets, testWidgets, finders, and matchers. This chapter focuses on inspecting the widget tree after you build or change it.
A widget test can check more than whether text exists. You can read a widget’s properties with tester.widget<T>(). You can read the mutable State object of a StatefulWidget with tester.state<T>().
Reading widget properties
tester.widget<T>(finder) returns the widget matched by a finder. Use the widget’s public properties to check how it was configured.
For example, a Text widget stores its displayed string in its data property. A CircularProgressIndicator has properties such as value and strokeWidth.
The finder should identify one widget, and the generic type should match the widget you expect.
import 'package:flutter/material.dart';
import 'package:flutter_test/flutter_test.dart';
class Counter extends StatefulWidget {
const Counter({super.key});
@override
State<Counter> createState() => _CounterState();
}
class _CounterState extends State<Counter> {
int count = 0;
@override
Widget build(BuildContext context) {
return Scaffold(
body: Text('Count: $count', key: const Key('count')),
floatingActionButton: FloatingActionButton(
key: const Key('increment'),
onPressed: () => setState(() => count++),
child: const Icon(Icons.add),
),
);
}
}
void main() {
testWidgets('inspects the displayed widget and its state', (tester) async {
await tester.pumpWidget(const MaterialApp(home: Counter()));
final text = tester.widget<Text>(
find.byKey(const Key('count')),
);
expect(text.data, 'Count: 0');
final state = tester.state<_CounterState>(
find.byType(Counter),
);
expect(state.count, 0);
await tester.tap(find.byKey(const Key('increment')));
await tester.pump();
final updatedText = tester.widget<Text>(
find.byKey(const Key('count')),
);
expect(updatedText.data, 'Count: 1');
expect(state.count, 1);
});
}tester.widget reads the current widget configuration in the tree. After pump, retrieve the widget again when you need to inspect its updated properties.
Reading state
A State object stores mutable data used by a StatefulWidget. The tester.state method returns the state associated with a matching widget.
In the example, _CounterState is the state type, and count is its mutable field. The test reads count before and after the tap.
Reading state can help diagnose a failure or verify a complex internal transition. However, most tests should prefer observable behavior. Checking Count: 1 is usually more valuable than checking the private field count, because users see the text, not the field.
Choosing what to inspect
Use these guidelines:
- Inspect a widget when its configuration affects behavior or appearance.
- Inspect state when a failure is difficult to understand through the UI.
- Use visible output and enabled actions for normal behavior tests.
- Use a unique key or a narrow finder before calling
tester.widgetortester.state. - Call
pumpafter an interaction before reading updated widgets or state.
Exercise
Why should a test call pump before reading the counter's updated Text widget after tapping the button?
Show answer
The tap changes state, but pump processes the rebuild so the widget tree contains the updated text.