I am trying to achieve the conditional showing of the screen when the app opens.
In main.dart
return Materialapp(
...
home: WelcomePage()
...
);
In WelcomePage.dart
isLoggedin() async {
prefs = await SharedPreferences.getInstance();
if (prefs.getString('username') == null) {
Navigator.of(context).pushAndRemoveUntil(
MaterialPageRoute(builder: (BuildContext context) => LoginPage()),
(Route<dynamic> route) => false);
}
}
Issue:
- The user will open the app, after the splash screen, the WelcomePage screen will show momentarily then redirects the user to Login screen.
This code does what I needed, but what I really want.
What I want is the app will not show the WelcomePage screen if the user has no saved preference in the app, instead, there is a code that checks whether the app will show the Login screen or the WelcomePage screen.
UPDATE
First, I really appreciate the answers given. It is both helpful but I think the other answer is the one I am thinking of. But I do it the way like this:
main.dart
Future main() async {
WidgetsFlutterBinding.ensureInitialized();
SharedPreferences prefs = await SharedPreferences.getInstance();
_username = prefs.getString("username");
...
}
return Materialapp(
...
home: _username == null ? LoginPage() : WelcomePage()
...
);
I don't think if it is one of the right way to do this. Any comments/suggestions will be appreciated.