Flutter Can';即使小部件在树中,也不能按键查找它

Flutter Can';即使小部件在树中,也不能按键查找它,flutter,flutter-test,Flutter,Flutter Test,在测试中,即使小部件在小部件树中,也无法通过键找到小部件的可能原因是什么? 因此,在我的应用程序中,我有一些类别的网站,每个类别都有一个+按钮,用户可以添加自己的网站。现在,我想测试当按下+按钮时页面是否正确打开。如果我这样做: wait tester.点击(find.byIcon(Icons.add)); 测试失败是因为它发现了太多的小部件,并且不知道要点击哪一个。所以我想我应该把+按钮的键包装在一个容器中,如下所示: 容器( key:ValueKey('add\u website\u'+c

在测试中,即使小部件在小部件树中,也无法通过键找到小部件的可能原因是什么?

因此,在我的应用程序中,我有一些类别的网站,每个类别都有一个+按钮,用户可以添加自己的网站。现在,我想测试当按下+按钮时页面是否正确打开。如果我这样做:

wait tester.点击(find.byIcon(Icons.add));
测试失败是因为它发现了太多的小部件,并且不知道要点击哪一个。所以我想我应该把+按钮的键包装在一个容器中,如下所示:

容器(
key:ValueKey('add\u website\u'+category.toLowerCase()),
child:AddWebsiteButton(),//包含图标(Icons.add)的可点击小部件
)
现在在我的测试中,我调用:

expect(find.byKey(ValueKey('add\u website\u learning'))、findsOneWidget);
瞧,它失败了:

══╡ EXCEPTION CAUGHT BY FLUTTER TEST FRAMEWORK ╞════════════════════════════════════════════════════
The following TestFailure object was thrown running a test:
  Expected: exactly one matching node in the widget tree
  Actual: _KeyFinder:<zero widgets with key [<'add_website_learning'>] (ignoring offstage widgets)>
   Which: means none were found but one was expected
我试图通过调用
ensureavibilible
来确保它在视图中,但这也无法说明它找不到小部件

我做错了什么

...
└KeyedSubtree-[Key <[<add_website_learning>]>]
  └AutomaticKeepAlive(state: _AutomaticKeepAliveState#470b4(handles: no notifications ever received))
    └KeepAlive(keepAlive: false)
      └NotificationListener<KeepAliveNotification>
        └IndexedSemantics(index: 4, renderObject: RenderIndexedSemantics#02bbd relayoutBoundary=up3 NEEDS-PAINT)
          └RepaintBoundary(renderObject: RenderRepaintBoundary#8170e relayoutBoundary=up4 NEEDS-PAINT)
            └Container-[<add_website_learning>]
              └Tooltip("Add website", dependencies: [_LocalizationsScope-[GlobalKey#55b7a], _InheritedTheme, TickerMode-[<CrossFadeState.showSecond>]], state: _TooltipState#87315(ticker inactive))
                └GestureDetector(startBehavior: start)
...