flutter go_router不良状态:刷新后可依赖的肘发射状态时没有元素

发布于 2025-01-25 20:38:35 字数 2791 浏览 1 评论 0原文

有3个Cubit流合并为一个。

GoRouter _router = GoRouter(
refreshListenable: GoRouterRefreshStream(StreamGroup.merge(
  [
    GetIt.I<AuthBloc>().stream,
    GetIt.I<SignUpFormCubit>().stream,
    GetIt.I<SignInFormCubit>().stream,
  ],
)),
// redirect to the login page if the user is not logged in
redirect: (state) {
  // if the user is not logged in, they need to login
  final loggedIn = GetIt.I<FirebaseAuth>().currentUser != null;
  final loggingIn = (state.subloc == Routes.auth ||
      state.subloc == Routes.signUp ||
      state.subloc == Routes.signIn);
  // if (!loggedIn) return loggingIn ? null : Routes.auth;
  // todo delete
  if (!loggedIn) return loggingIn ? null : Routes.auth;

  // if the user is logged in but still on the login page, send them to
  // the home page
  if (loggingIn) return Routes.home;

  // no need to redirect at all
  return null;
},
routes: <GoRoute>[
  GoRoute(
    path: Routes.home,
    builder: (context, GoRouterState state) => InitialPage(),
  ),
  GoRoute(
    path: Routes.auth,
    builder: (context, GoRouterState state) => AuthPage(),
  ),
  GoRoute(
    path: Routes.signUp,
    builder: (context, GoRouterState state) => SignUpPage(),
  ),
  GoRoute(
    path: Routes.signIn,
    builder: (context, GoRouterState state) => SignInPage(),
  ),
],
);

auth 路线可以转移到路线中的符号:

context.push(Routes.signIn);

从中返回 auth 路线

一直按预期工作,直到Cubit散发出某种状态为止。

signInformCubit (看一下刷新的可刷新)每次有验证错误时都会发出状态,例如邮件不正确。

问题在于,当任何状态发出后,按钮停止按预期工作:

The following StateError was thrown building Builder(dirty):
Bad state: No element

The relevant error-causing widget was: 
  Spacing Spacing:file:///Users/sergiyvergun/IdeaProjects/Fleengo/lib/app/core.dart:160:16
When the exception was thrown, this was the stack: 
#0      List.last (dart:core-patch/growable_array.dart:365:5)
#1      GoRouterDelegate.location (package:go_router/src/go_router_delegate.dart:192:32)
#2      GoRouterDelegate._builder (package:go_router/src/go_router_delegate.dart:647:33)
#3      GoRouterDelegate.build (package:go_router/src/go_router_delegate.dart:215:41)
#4      Builder.build (package:flutter/src/widgets/basic.dart:7398:48)
#5      StatelessElement.build (package:flutter/src/widgets/framework.dart:4827:28)
#6      ComponentElement.performRebuild (package:flutter/src/widgets/framework.dart:4754:15)
#7      Element.rebuild (package:flutter/src/widgets/framework.dart:4477:5)

如何解决此问题?

There is go_router with 3 cubit streams merged into one.

GoRouter _router = GoRouter(
refreshListenable: GoRouterRefreshStream(StreamGroup.merge(
  [
    GetIt.I<AuthBloc>().stream,
    GetIt.I<SignUpFormCubit>().stream,
    GetIt.I<SignInFormCubit>().stream,
  ],
)),
// redirect to the login page if the user is not logged in
redirect: (state) {
  // if the user is not logged in, they need to login
  final loggedIn = GetIt.I<FirebaseAuth>().currentUser != null;
  final loggingIn = (state.subloc == Routes.auth ||
      state.subloc == Routes.signUp ||
      state.subloc == Routes.signIn);
  // if (!loggedIn) return loggingIn ? null : Routes.auth;
  // todo delete
  if (!loggedIn) return loggingIn ? null : Routes.auth;

  // if the user is logged in but still on the login page, send them to
  // the home page
  if (loggingIn) return Routes.home;

  // no need to redirect at all
  return null;
},
routes: <GoRoute>[
  GoRoute(
    path: Routes.home,
    builder: (context, GoRouterState state) => InitialPage(),
  ),
  GoRoute(
    path: Routes.auth,
    builder: (context, GoRouterState state) => AuthPage(),
  ),
  GoRoute(
    path: Routes.signUp,
    builder: (context, GoRouterState state) => SignUpPage(),
  ),
  GoRoute(
    path: Routes.signIn,
    builder: (context, GoRouterState state) => SignInPage(),
  ),
],
);

from auth route is a possibility to move to sign in route:

context.push(Routes.signIn);

and reversed go back from sign in to auth route

it works as expected until the cubit emits some state.

SignInFormCubit(take a look to refreshListenable) emits a state every time there is some validation error e.g. mail is not correct.

The problem is that when any state is emitted back button stops working as expected:
enter image description here

The following StateError was thrown building Builder(dirty):
Bad state: No element

The relevant error-causing widget was: 
  Spacing Spacing:file:///Users/sergiyvergun/IdeaProjects/Fleengo/lib/app/core.dart:160:16
When the exception was thrown, this was the stack: 
#0      List.last (dart:core-patch/growable_array.dart:365:5)
#1      GoRouterDelegate.location (package:go_router/src/go_router_delegate.dart:192:32)
#2      GoRouterDelegate._builder (package:go_router/src/go_router_delegate.dart:647:33)
#3      GoRouterDelegate.build (package:go_router/src/go_router_delegate.dart:215:41)
#4      Builder.build (package:flutter/src/widgets/basic.dart:7398:48)
#5      StatelessElement.build (package:flutter/src/widgets/framework.dart:4827:28)
#6      ComponentElement.performRebuild (package:flutter/src/widgets/framework.dart:4754:15)
#7      Element.rebuild (package:flutter/src/widgets/framework.dart:4477:5)

How to fix this?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

拧巴小姐 2025-02-01 20:38:36

我遇到了不良状态的同样问题:没有元素。我的问题是在基本或主路线上使用“重定向”参数。当我从求解的基本路线上删除“重定向”参数时。

但是这个东西没有任何地方

I encountered the same problem of Bad state: No element. My problem was using the "redirect" parameter on the base or main route. When I removed the "redirect" param from the base route it solved.

But this thing was mentioned nowhere

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文