1

Question: title

I need some sort of session checking like sessionId ?? index.html, I have no idea how to do it. Router.dashboardView and Router.loginView are static const String generated by AutoRoute package as namedRoute. The current code is working fine if I access it from the root, but I can bypass it if I write the route name directly on the Url.

Example (root): https://webapp.web.app/ => automatically give me https://webapp.web.app/#/loginView

Example (write route name on Url): https://webapp.web.app/#/dashboardView (bypassing loginView)

Thanks for any of your suggestions and idea.

This is my current code,

main.dart:

class MyApp extends StatefulWidget {
  @override
  _MyAppState createState() => _MyAppState();
}

class _MyAppState extends State<MyApp> {
  bool isLogin;
  String initialRoute;

  @override
  void initState() {
    super.initState();
    initialRoute = checkSession();
  }

  String checkSession() {
    isLogin = WebStorage.instance.sessionId != null;

    return isLogin ? Router.dashboardView : Router.loginView;
  }

  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      initialRoute: initialRoute,
      onGenerateRoute: Router.onGenerateRoute,
      navigatorKey: Router.navigatorKey,
    );
  }
}

dashboard_view.dart:

class DashboardView extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      body: Center(
        child: Text('DashboardView'),
      ),
    );
  }
}

login_view.dart:

class LoginView extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      body: Center(
        child: Text('LoginView'),
      ),
    );
  }
}
Federick Jonathan
  • 2,726
  • 2
  • 13
  • 26

0 Answers0