Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can't enter text in flutter test

I've been trying to test an element that when an IconButton is pressed, a Text object changes to a TextFormField. When I try to test this I get the following error:

A Timer is still pending even after the widget tree was disposed.
'package:flutter_test/src/binding.dart': Failed assertion: line 672 pos 7:
'_fakeAsync.nonPeriodicTimerCount == 0'

Despite the widget not using any timers that I'm aware of?

Example code:

await tester.tap(find.byType(IconButton));
await tester.pump();

expect(find.byType(TextFormField), findsOneWidget);
await tester.pump();
await tester.enterText(find.byType(EditableText), "0.2");

I'm really not sure what's causing the issue but if I remove the last 2 lines it runs fine (although I don't get to actually test my input).

like image 389
Alistair McIntyre Avatar asked Apr 26 '18 04:04

Alistair McIntyre


2 Answers

The issue was that the text field used had a 300ms delay so that the keyboard can scroll into view and then the app can scroll to the textfield. I completely ignored that the text field has this delay and didn't account for it.

Adding

await tester.pump(Duration(milliseconds:400));

worked exactly as intended.

like image 75
Alistair McIntyre Avatar answered Oct 16 '22 07:10

Alistair McIntyre


Assign Key To the EditableText

new EditableText(
  key: new Key('mySpecialEditableText1234'),
  controller: myCtrl,
  style: myStyle,
  focusNode: myFocusNode,
)

and access this widget in your test as:

find.byKey(new Key('mySpecialEditableText1234'))

If this doesn't find your Widget, your tester instance is probably not pumping the correct widget

NOTE: Make sure you are going through correct navigation path. You cannot directly pump a widget that is not first in the tree

like image 6
Arnold Parge Avatar answered Oct 16 '22 07:10

Arnold Parge