gpt4 book ai didi

flutter - 小部件构建期间如何在抖动中导航?

转载 作者:行者123 更新时间:2023-12-03 03:07:38 25 4
gpt4 key购买 nike

我试图检测到该用户不再通过身份验证,并将用户重定向到登录名。这就是我的做法

  Widget build(BuildContext context) {
return FutureBuilder(
future: _getData(context),
builder: (context, snapshot) {
try {
if (snapshot.hasError && _isAuthenticationError(snapshot.error)) {
Navigator.push(context, MaterialPageRoute(builder: (context) => LoginView()));
}

不幸的是,在构建中进行导航无法正常工作。它抛出此错误
flutter: setState() or markNeedsBuild() called during build.
flutter: This Overlay widget cannot be marked as needing to build because the framework is already in the
flutter: process of building widgets. A widget can be marked as needing to be built during the build

我不能只返回 LoginView小部件,因为父小部件包含应用程序栏和 float 按钮,并且需要在没有这些控件的情况下显示登录 View 。

有可能做到吗?

最佳答案

将其包装在Future.microtask中。这会将它安排在下一个异步任务周期(即build完成之后)进行。

Future.microtask(() => Navigator.push(
context,
MaterialPageRoute(builder: (context) => LoginView())
));

关于flutter - 小部件构建期间如何在抖动中导航?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59804090/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com