admin管理员组文章数量:1353618
In my main.dart I have defined my router:
Widget build(BuildContext context) {
final router = ref.read(routerProvider);
Then
return MaterialApp.router(
routerConfig: router,
In my router I have :
final routerProvider = Provider<GoRouter>((ref) {
final authState = ref.watch(authProvider);
return GoRouter(
initialLocation: '/',
redirect: (context, state) {
if (authState != null) {
if (authState.freeTrialOpted != null) {
return '/monks';
} else {
ref.read(authProvider.notifier).signOut();
return '/wisdom';
}
}
return null;
},
routes: [
GoRoute(
path: '/',
builder: (context, state) => const WisdomScreen(),
),
GoRoute(
path: '/wisdom',
builder: (context, state) => const WisdomScreen(),
),
GoRoute(
path: '/monks',
builder: (context, state) => const MonksScreen(),
),
GoRoute(
path: '/setting',
name: 'setting',
builder: (context, state) {
print("are we here");
return SettingsScreen();
},
),
],
errorBuilder: (context, state) => Scaffold(
body: Center(
child: Text('Page not found: '),
),
),
);
});
And then I am trying to use it in a screen like this :
try {
context.goNamed(
'setting',
);
} catch (e) {
print(
"Navigation error: $e");
}
But nothing is happening I am not even getting any error message. Screen doesn't navigate at all. And I am not able to print this print("are we here"); in my route definition. I have tried .go, .pushNamed but nothing is working. Redirect works fine but after that I get this
In my main.dart I have defined my router:
Widget build(BuildContext context) {
final router = ref.read(routerProvider);
Then
return MaterialApp.router(
routerConfig: router,
In my router I have :
final routerProvider = Provider<GoRouter>((ref) {
final authState = ref.watch(authProvider);
return GoRouter(
initialLocation: '/',
redirect: (context, state) {
if (authState != null) {
if (authState.freeTrialOpted != null) {
return '/monks';
} else {
ref.read(authProvider.notifier).signOut();
return '/wisdom';
}
}
return null;
},
routes: [
GoRoute(
path: '/',
builder: (context, state) => const WisdomScreen(),
),
GoRoute(
path: '/wisdom',
builder: (context, state) => const WisdomScreen(),
),
GoRoute(
path: '/monks',
builder: (context, state) => const MonksScreen(),
),
GoRoute(
path: '/setting',
name: 'setting',
builder: (context, state) {
print("are we here");
return SettingsScreen();
},
),
],
errorBuilder: (context, state) => Scaffold(
body: Center(
child: Text('Page not found: '),
),
),
);
});
And then I am trying to use it in a screen like this :
try {
context.goNamed(
'setting',
);
} catch (e) {
print(
"Navigation error: $e");
}
But nothing is happening I am not even getting any error message. Screen doesn't navigate at all. And I am not able to print this print("are we here"); in my route definition. I have tried .go, .pushNamed but nothing is working. Redirect works fine but after that I get this
Share Improve this question asked Mar 31 at 16:38 Sahil SinghSahil Singh 2411 gold badge6 silver badges18 bronze badges 2- Is settings supposed to be accessible when logged in or not ? – Daniel Onadipe Commented Mar 31 at 17:00
- No its only accessible when logged in. – Sahil Singh Commented Mar 31 at 18:53
1 Answer
Reset to default 0Only monks
and wisdom
screen are accessible when logged in according to your redirect, assuming setting is supposed to be accessible when logged in,
Rather than the else statement after
if(freeTrialOpted != null)
Do a proper condition check for when you actually want to redirect to wisdom if user is logged in.
if (authState.freeTrialOpted != null) {
return '/monks';
}
if (// proper check for wisdom navigation ) {
ref.read(authProvider.notifier).signOut();
return '/wisdom';
}
Basically you want to make sure null
is returned if all the conditions above are not met so the original page is navigated to.
本文标签: flutterGoRouter won39t navigate to a new screen and there is no error messageStack Overflow
版权声明:本文标题:flutter - GoRouter won't navigate to a new screen and there is no error message - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1743934013a2564359.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论