Member-only story
Why AbsorbPointers Are Dangerous In Flutter Widget Tests
I introduced an AbsorbPointer widget which broke my app but the tests to prevent exactly this didn’t fail.
Once again, I stumbled across a weird issue with Flutter widget tests. They are not as reliable as you might think. This time I found a problem with a combination of AbsorbPointer
and TextField
.
Here is a pretty simple example:
@override
Widget build(BuildContext context) {
return AbsorbPointer(absorbing: true, child: TextField());
}
We have a TextField
widget wrapped inside an AbsorbPointer
widget. The purpose of the AbsorbPointer
is to prevent the TextField
from getting focused. The keyboard doesn’t open and you cannot enter text.
When you launch this on any platform (mobile, web desktop), you’ll notice that you cannot focus the input field. Works as expected!
But now, let’s write a simple widget test to ensure the correct functionality. Again some code:
testWidgets('Absorb pointer test', (WidgetTester tester) async {
await tester.pumpWidget(const MyApp());
await tester.enterText(find.byType(TextField), "test");
await tester.pump();
expect(find.text("test"), findsNothing);
});