Flutter的`tester.scrollUntilVisible`抛出“Bad state: Too many elements”异常。

7

我在我的材料设计应用程序中将以下元素封装到单个ListView中:

        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
            ])));

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

    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,
    );

它会抛出以下异常:

══╡ 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)

欢迎提供任何建议和见解!

2个回答

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

享受吧!


1
dragUntilVisible()替换scrollUntilVisible()可以解决问题!我找不到任何关于scrollUntilVisible()的信息。这是一个应该从框架中删除的过时API吗?

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接