Can't enter text in flutter test

4,784

Solution 1

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.

Solution 2

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

Share:
4,784
Alistair McIntyre
Author by

Alistair McIntyre

Updated on December 04, 2022

Comments

  • Alistair McIntyre
    Alistair McIntyre over 1 year

    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).

  • Alistair McIntyre
    Alistair McIntyre about 6 years
    The tester instance was finding the widget just fine, I'll post the solution as another answer.