使用go_router传递验证数据
在我的项目中,我实现了管理状态的提供者方法,我想与GO Router软件包共享Auth Provister信息,以使用户登录
class MyApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
return MultiProvider(
providers: [
ChangeNotifierProvider(
create: (ctx) => Auth(),
),
ListenableProxyProvider<Auth, AppRouter>(
update: (_, authObj, prevOrders) =>
AppRouter(authObj)
),
}
和在我的Applouter类中,我有一个限制者可以获取auth Data:
class AppRouter with ChangeNotifier {
final Auth authData;
AppRouter(this.authData);
final router = GoRouter(
initialLocation: '/',
routes: [
GoRoute(
name: root,
path: '/',
builder: (context, state) => TabsScreen(),
// redirect: (state) => state.namedLocation(authScreen),
),
GoRoute(
name: mainScreen,
path: '/main-screen',
builder: (context, state) => HomeScreen(),
),
GoRoute(
name: authscreen,
path: '/auth-screen',
builder: (context, state) => AuthScreen(),
),
],
redirect: (state) {
final loginLoc = state.namedLocation(authScreen);
final loggingIn = state.subloc == loginLoc;
var loggedIn = authData.isLoggedIn;
if (!loggedIn && !loggingIn) return loginLoc;
if (loggedIn && (loggingIn)) return root;
return null;
},
但是我可以't访问我的班级中的authdata,我会收到此错误:
The instance member 'authData' can't be accessed in an initializer.
Try replacing the reference to the instance member with a different expression
In my project, I implement the Provider method to manage state, and I'd like to share auth provider info with the go router package to keep users logged in
class MyApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
return MultiProvider(
providers: [
ChangeNotifierProvider(
create: (ctx) => Auth(),
),
ListenableProxyProvider<Auth, AppRouter>(
update: (_, authObj, prevOrders) =>
AppRouter(authObj)
),
}
and within my AppRouter class I have a constructer to get auth data :
class AppRouter with ChangeNotifier {
final Auth authData;
AppRouter(this.authData);
final router = GoRouter(
initialLocation: '/',
routes: [
GoRoute(
name: root,
path: '/',
builder: (context, state) => TabsScreen(),
// redirect: (state) => state.namedLocation(authScreen),
),
GoRoute(
name: mainScreen,
path: '/main-screen',
builder: (context, state) => HomeScreen(),
),
GoRoute(
name: authscreen,
path: '/auth-screen',
builder: (context, state) => AuthScreen(),
),
],
redirect: (state) {
final loginLoc = state.namedLocation(authScreen);
final loggingIn = state.subloc == loginLoc;
var loggedIn = authData.isLoggedIn;
if (!loggedIn && !loggingIn) return loginLoc;
if (loggedIn && (loggingIn)) return root;
return null;
},
however I can't access authData within my class and I get this error :
The instance member 'authData' can't be accessed in an initializer.
Try replacing the reference to the instance member with a different expression
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
本文帮助我解决了问题,还有有关使用go_router实施导航2.0的完整指南:
flutter导航器2.0:使用go_router
this article helped me solve my problem and Also there is complete guide about implementing Navigation 2.0 using Go_Router :
Flutter Navigator 2.0: Using go_router
这是正常的。即使您在想:“当我将我的authdata分配给DART时,重定向也会发生。对于DART,您正在尝试在创建路由器的时刻使用Authdata的值,因此未分配它。例如,如果Authdata是一个静态字段,并且已经分配了,则可以执行此操作。
您可能可以通过阅读此书来实现您要实现的目标:
https://gorouter.dev/parameters
编辑:
这是我要做的代码段:
注意:记住要始终通过这样的状态:
This is normal. Even if you're thinking:"The redirect will happen when I'll have my authData assigned" Dart don't see it like this. For dart, you're trying to use the value of authData in the moment of the creation of the router, so, it is not assigned. For example, you could do this if authData was a static field and it was already assigned.
You can probably achieve what you're trying to achieve by reading this:
https://gorouter.dev/parameters
EDIT:
Here is a code snippet of what i would do:
Note: Remember to always pass the state like this: