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!
The error seems to occur when there are more than one Scrollable in the widget tree. Then Flutter doesn't know which one to scroll. You can solve that by first finding the correct Scrollable and telling scrollUntilVisible to use it:
// Scroll Save button into view
final listFinder = find.byType(Scrollable).last; // take last because the tab bar up top is also a Scrollable
expect(listFinder, findsOneWidget);
await tester.scrollUntilVisible(acceptButtonFinder, 100, scrollable: listFinder);
Enjoy!
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?
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With