1

I have 2 web pages on my proyect

enter image description here

enter image description here

I need to manage the user session. If the user is logged it appears the home page, and if the user is not logged it appears the login page.

I am using a provider to check the state of the session

  @override
  Widget build(BuildContext context) {
    return Consumer<LoginGeneralNotifier>(
      builder: (context, appGenaralNotifier, _) {
        return appGenaralNotifier.getLogged()
            ? HomePage()
            : LoginPage();
      },
    );
  }
}

The problem is that the url does not change after I am logged

Ezequiel
  • 331
  • 4
  • 6

3 Answers3

0

You may want to use Navigator.of(context).pushNamed(HomePageNamedRoute);

This can be done in initState as shown here: Flutter Redirect to a page on initState

Denis G
  • 511
  • 4
  • 11
0

what I do is

I have created a class where I am keeping all the global variables

there I create a variable bool loginStatus;

and create two methods get and set for handling the value

and whenever login is successful I set the value to true loginStatus=true;

and on the new page in init() method I am checking the value of variable if value is false then throw the user to login page else continue

class GlobalValues{
  static bool loginStatus;


  static void setLoginStatus(bool val) {
    loginStatus = val;
  }

  static bool getLoginStatus() {
    return loginStatus == null ? false : loginStatus;
  }
}

on login page if login is successful

GlobalValues.setLoginStatus(true);

on other class pages in init method

  @override
  void initState() {
     if (GlobalValues.getLoginStatus()) {
       // call your login page class here
       //Navigator.of(context).pushNamed(LoginPageNamedRoute);
       Navigator.pop(context);
    }

    super.initState();
  }
Vicky Salunkhe
  • 9,869
  • 6
  • 42
  • 59
0

Global variables are highly-frowned upon in the programming world (unless absolutely no other alternatives are available). Use user sessions instead. Check out FlutterSession. The package adds user session support in Flutter and is easy to use.

// Store value to session
await FlutterSession().set("token", myJWTToken);

// Retrieve item from session
dynamic token = await FlutterSession().get("token");
Jhourlad Estrella
  • 3,545
  • 4
  • 37
  • 66