How to navigate to another page on load in Flutter

I want to navigate to the login page if there is no logged in user, otherwise display the homepage. I thought of calling Navigator.of(context).push() conditionally inside the build method but that triggers an exception. Is there some method I'm missing that I can override?

Update to add the Homepage widget

class HomePage extends StatelessWidget {
final AppUser user;

const HomePage({Key key, this.user}) : super(key: key);

@override
Widget build(BuildContext context) {
return Scaffold(
    appBar: AppBar(
      title: Text('Rera Farm'),
      actions: <Widget>[
        PopupMenuButton(
          itemBuilder: (BuildContext context) {
            return <PopupMenuEntry>[
              PopupMenuItem(
                child: ListTile(
                  title: Text('Settings'),
                  onTap: () {
                    Navigator.pop(context);
                    Navigator.push(context,
                        MaterialPageRoute(builder: (BuildContext context) 
                      => SettingsPage()
                    ));
                  },
                ),
              ),
            ];
          },
        )
      ],
    ),
    body: _buildBody(context));
}

And the container

class HomePageContainer extends StatelessWidget {
@override
Widget build(BuildContext context) {
 return new StoreConnector<AppState, _ViewModel>(
   converter: _ViewModel.fromStore,
   builder: (BuildContext context, _ViewModel vm) {
     return HomePage(
      user: vm.user,
     );
   },
 );
 }
}

Upvotes: 7

Views: 10895

Answers (3)

Bo Hellgren
Bo Hellgren

Reputation: 181

Thanks, this is exactly what I needed. I might add that my LoginPage

  • uses Shared Pref to return logon data to HomePage
  • ends with
Navigator.pushReplacement(context, MaterialPageRoute(
    builder: (context) => const HomePage(title: 'xxx'),
),);

Upvotes: 0

Here's how I end up doing it. I do the checks in the main method, so the user sees the splash screen set in manifest while those weird checks are made:

void main() {
    WidgetsFlutterBinding.ensureInitialized();
    SharedPreferences.getInstance().then((instance) {
        _token = instance.getString("token");
        final _loggedIn = _token != null && token != "";
        runApp(MyApp(loggedIn: _loggedIn));
   });
}

Then in your app add the parameters to switch:

class MyApp extends StatelessWidget {
final bool loggedIn;

MyApp({this.key, this.loggedIn});

@override
Widget build(BuildContext context) {
    return MaterialApp(
        home: loggedIn ? HomePage() : LoginPage(),
    );
}
}

You can also use Navigator.pushReplacement() if you need to do it below MyApp(). Just posting it here for future generations.

Upvotes: 1

scottstoll2017
scottstoll2017

Reputation: 1124

You need to either use a ternary in the onTap if you're using the settings button or, if you just want it to automatically send the user to the correct page when the app starts, you can put the ternary in the MyApp build method.

If you are using the settings button and just want it to pop back to the previous page if the person is not logged in then you can change NotLoggedIn() to a pop.

For some strange reason SO is refusing to post the code when it is properly formatted with four spaces, exactly as it asks, so I'm just going to make a gist.

https://gist.github.com/ScottS2017/3288c7e7e9a014430e56dd6be4c259ab

Upvotes: 3

Related Questions