Closed jaumard closed 3 years ago
That's a standard Flutter issue. Assuming the following:
late final router = GoRouter(...);
then you can do the following:
onPressed: () => router.go(...);
Does that work for you?
P.S. don't for the late
keyword!
So I need a global variable to avoid this? It will works for sure but that's ugly :D
100% not. You can continue to define your router inside your app, e.g.
class MyApp {
...
late final router = GoRouter(...);
...
}
Yeah I put the router creation inside his own file ^^, something like this:
late GoRouter _router;
GoRouter createRouter(UserStore userStore) => _router = GoRouter(
//... all routes and redirection stuff
errorPageBuilder: (BuildContext context, GoRouterState state) {
return CustomTransitionPage<void>(
key: state.pageKey,
transitionsBuilder: (BuildContext context, Animation<double> animation, Animation<double> secondaryAnimation, Widget child) {
return FadeTransition(opacity: animation, child: child);
},
child: BgContainer(
child: IntroDialog(
child: Column(
mainAxisSize: MainAxisSize.min,
children: [
Text('404 page introuvable', style: context.textTheme.headline6),
const SizedBox(height: kSmallPadding),
TextButton(
onPressed: () {
_router.go('/');
},
child: const Text('Retourner sur le site'),
),
],
),
),
),
);
},
);
I have added the global variable (as private) and no more issues like this.
Cool.
I'm trying to do a 404 page not found screen with a button to return to the website:
When I click I get:
Look like router is not accessible from the context given to the error callback :/ did I miss something ?
I use version 2.2.1 on last flutter stable