flutter go_router不良状态:刷新后可依赖的肘发射状态时没有元素
有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:
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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我遇到了不良状态的同样问题:没有元素。我的问题是在基本或主路线上使用“重定向”参数。当我从求解的基本路线上删除“重定向”参数时。
但是这个东西没有任何地方
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