flutter `tester.scrollUntilVisible` 抛出“错误状态:元素太多”异常

Kok*_*Teh 8 unit-testing exception flutter flutter-listview

我将以下元素封装到ListView我的材料应用程序中:

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

当我运行包含以下代码片段的单元测试时:

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

它抛出以下异常:

\n
\xe2\x95\x90\xe2\x95\x90\xe2\x95\xa1 EXCEPTION CAUGHT BY FLUTTER TEST FRAMEWORK \xe2\x95\x9e\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\nThe following StateError was thrown running a test:\nBad state: Too many elements\n\nWhen the exception was thrown, this was the stack:\n#0      Iterable.single (dart:core/iterable.dart:656:24)\n#1      WidgetController.widget (package:flutter_test/src/controller.dart:69:30)\n#2      WidgetController.scrollUntilVisible.<anonymous closure> (package:flutter_test/src/controller.dart:1190:15)\n#3      WidgetController.scrollUntilVisible.<anonymous closure> (package:flutter_test/src/controller.dart:1188:39)\n#6      TestAsyncUtils.guard (package:flutter_test/src/test_async_utils.dart:71:41)\n#7      WidgetController.scrollUntilVisible (package:flutter_test/src/controller.dart:1188:27)\n#8      main.<anonymous closure> (file:///usr/src/flutter/flutter_app_layout/test/widget_test.dart:50:18)\n<asynchronous suspension>\n<asynchronous suspension>\n(elided 3 frames from dart:async and package:stack_trace)\n
Run Code Online (Sandbox Code Playgroud)\n

任何建议和见解表示赞赏!

\n

Joh*_*rug 5

Scrollable当小部件树中有多个时,似乎会发生该错误。那么Flutter就不知道该滚动哪一个了。您可以通过首先找到正确的Scrollable并告诉scrollUntilVisible使用它来解决这个问题:

   // 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);
Run Code Online (Sandbox Code Playgroud)

享受!


Kok*_*Teh 1

替换scrollUntilVisible()为即可dragUntilVisible()解决问题!我根本没有找到任何东西scrollUntilVisible()。这是一个过时的 API,应该从框架中删除吗?