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
Add a comment  | 

1 Answer 1

Reset to default 0

Only 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