Home > OS >  flutter `tester.scrollUntilVisible` throws "Bad state: Too many elements" exception
flutter `tester.scrollUntilVisible` throws "Bad state: Too many elements" exception

Time:07-06

I have the following elements encapsulated into a single ListView in my material app:

        home: Scaffold(
            appBar: AppBar(title: const Text("Flutter Layout")),
            body: ListView(children: [
              fibonacciSection,
              // a ListView supports app body scrolling when the app is run on a small device.
              Image.asset("images/lake.jpg",
                  width: 600,
                  height: 240,
                  fit: BoxFit
                      .cover), // BoxFit.cover tells the framework that the image should be as small as possible but cover its entire render box.
              titleSection,
              buttonsSection,
              textSection,
              statesSection
            ])));

And when I run the unit tests which contain the following code snippet:

    await tester.pumpWidget(const MyApp(key: Key("StateManagemetTests")));
    final listFinder = find.byType(Scrollable);
    final itemFinder = find.byType(TapboxB);
    // Scroll until the item to be found appears.
    await tester.scrollUntilVisible(
      itemFinder,
      500.0,
      scrollable: listFinder,
    );

It throws the following exception:

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

When the exception was thrown, this was the stack:
#0      Iterable.single (dart:core/iterable.dart:656:24)
#1      WidgetController.widget (package:flutter_test/src/controller.dart:69:30)
#2      WidgetController.scrollUntilVisible.<anonymous closure> (package:flutter_test/src/controller.dart:1190:15)
#3      WidgetController.scrollUntilVisible.<anonymous closure> (package:flutter_test/src/controller.dart:1188:39)
#6      TestAsyncUtils.guard (package:flutter_test/src/test_async_utils.dart:71:41)
#7      WidgetController.scrollUntilVisible (package:flutter_test/src/controller.dart:1188:27)
#8      main.<anonymous closure> (file:///usr/src/flutter/flutter_app_layout/test/widget_test.dart:50:18)
<asynchronous suspension>
<asynchronous suspension>
(elided 3 frames from dart:async and package:stack_trace)

Any advice and insight is appreciated!

CodePudding user response:

Replacing scrollUntilVisible() with dragUntilVisible() solves the problem! I don't find anything at all for scrollUntilVisible(). Is that an outdated API which should be removed from the framework?

  • Related