Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to navigate to another page on load in Flutter

Tags:

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,
     );
   },
 );
 }
}
like image 583
Edmore M Gonese Digolodollarz Avatar asked Nov 27 '18 01:11

Edmore M Gonese Digolodollarz


People also ask

How do I navigate to previous screen in flutter?

The user can click the button again to come back to the previous page. We will create two screens, namely main. dart and SecondPage. dart .

How do I navigate to the same page in flutter?

If you are in the same page you want to push, you could do: Navigator. popAndPushNamed(context, C); Which will pop current page (C) and push C.

How do you go from one activity to another activity in flutter?

To navigate from one screen to another, you must use the Navigator . Here is an example to navigate on another page which class name is AnotherPage as you may see. Show activity on this post. You have to use navigator.


Video Answer


1 Answers

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

like image 131
scottstoll2017 Avatar answered Oct 12 '22 22:10

scottstoll2017