Flutter tester..tap not working. How to solve "Bad state: No element" error?

Viewed 3672

I'm writing a widget test in my Flutter app. I'able to find the button using key or text or widget type but when I tap it, it gives Bad State No element error. Below is my test source code. I can also see the element in tester object while debugging under allWidgets item.

class MockSpeechRecognizer extends Mock implements SpeechRecognizer {}

void main() {
  final TestWidgetsFlutterBinding binding = TestWidgetsFlutterBinding.ensureInitialized();
  setUpAll(() {
    const MethodChannel('plugins.flutter.io/shared_preferences')
        .setMockMethodCallHandler((MethodCall methodCall) async {
      if (methodCall.method == 'getAll') {
        return <String, dynamic>{}; // set initial values here if desired
      }
      return null;
    });

    setupLocator();
  });
  group('DashboardView Test | ', () {
    testWidgets('Build Dashboard view and change keyword', (WidgetTester tester) async {
      final Preferences preferences=Preferences();
      preferences.keyword='hello';
      final MockSpeechRecognizer speechService = MockSpeechRecognizer();
      when(speechService.isServiceRunning).thenReturn(true);

      locator.unregister<SpeechRecognizer>();
      locator.registerLazySingleton<SpeechRecognizer>(() => speechService);

      locator<LocalStorageService>().setUserPreferences(preferences);

      // Build our app and trigger a frame.
      binding.window.physicalSizeTestValue = Size(600, 300);
      await tester.pumpWidget(MaterialApp(home:Dashboard()));

      await tester.pumpAndSettle();

      expect(find.byType(Dashboard),findsOneWidget);

      await tester.tap(find.byKey(const Key('ChangeKeyword')));
    });
  });
}


══╡ EXCEPTION CAUGHT BY FLUTTER TEST FRAMEWORK ╞════════════════════════════════════════════════════
The following StateError was thrown running a test:
Bad state: No element

When the exception was thrown, this was the stack:
#0      Iterable.single (dart:core/iterable.dart:554:25)
#1      WidgetController._getElementPoint (package:flutter_test/src/controller.dart:646:47)
#2      WidgetController.getCenter (package:flutter_test/src/controller.dart:618:12)
#3      WidgetController.tap (package:flutter_test/src/controller.dart:256:18)
#4      main.<anonymous closure>.<anonymous closure> (file:///E:/Siak/Meow/meow-phone-finder/test/Widgets/dashboardView_test.dart:52:20)
<asynchronous suspension>
#5      main.<anonymous closure>.<anonymous closure> (file:///E:/Siak/Meow/meow-phone-finder/test/Widgets/dashboardView_test.dart)
#6      testWidgets.<anonymous closure>.<anonymous closure> (package:flutter_test/src/widget_tester.dart:140:29)
<asynchronous suspension>
#7      testWidgets.<anonymous closure>.<anonymous closure> (package:flutter_test/src/widget_tester.dart

below is the result when i called debugDumpApp()

1 Answers

Where do you define the Button, have you set the Key parameter?

await tester.pumpWidget(MaterialApp(home: 
  Container(child: 
    Button(
      key: Key("ChangeKeyword"),
      onTap: () => {},
      child: Text("Press me"),
    ),
  ),
));
Related