Login and go to other page after

2,303 views
Skip to first unread message

Mattias Månsson

unread,
Jun 11, 2019, 4:58:07 PM6/11/19
to Flutter Development (flutter-dev)
I have a small problem I'm thinking of how to best solve it in flutter. I could probably solve it myself with some hack, but I'm sure there is a more beautiful flutter way, so here goes...

I have a button in my UI where if clicked I want to tell the user that to go to this page, he should first login. Right now I show a dialog telling this with yes/no and yes redirects the user to the login page. But after logging it, it will go back to the main page and the button has to be clicked again. Is there a nicer way to do this in flutter so I can get the flow main page->dialog->login->next page?

Sócrates Díaz Severino

unread,
Jun 11, 2019, 5:20:33 PM6/11/19
to Flutter Development (flutter-dev)
One of the ways to solve this problem is using the Navigation object provided by Flutter. You can pop a widget with a return value and await for it in the widget that pushed it. That way you could achieve the navigation flow that you described.

You can find more info here: https://api.flutter.dev/flutter/widgets/Navigator-class.html (specifically in the section "Routes can return a value")

Mattias Månsson

unread,
Jun 15, 2019, 8:46:15 PM6/15/19
to Flutter Development (flutter-dev)
Tried what you're saying but there is something with the state of the app and async calls I don't get right. Basically when the user presses the button that requires login, I call a method like this (removed the uninteresting code):

Future<void> _openSignInDialog() {
 
return showDialog<void>(
    context
: context,
    builder: (BuildContext context) {
     
return AlertDialog(
        title
: Text('Please sign in'),
        content: const Text(
           
'...'),
        actions: <Widget>[
         
FlatButton.icon(
            icon
: Icon(Icons.check_circle, color: Colors.green),
            label: Text('Yes'),
            onPressed: () {
             
// Replace this dialog with the login page
              Navigator.pushReplacementNamed(context, '/login').then((_) {
               
Navigator.of(context).pushNamed('/otherpage');
              });
            },
          ),
          FlatButton.icon(
            icon
: Icon(Icons.cancel, color: Colors.red),
            label: Text('No'),
            onPressed: () {
             
Navigator.pop(context);
            },
          ),
        ],
      );
    },
  );
}

But if I run this, it will crash after returning from the login page with this exception:

E/flutter (27459): [ERROR:flutter/lib/ui/ui_dart_state.cc(148)] Unhandled Exception: Looking up a deactivated widget's ancestor is unsafe.
E/flutter (27459): At this point the state of the widget's element tree is no longer stable. To safely refer to a widget's ancestor in its dispose() method, save a reference to the ancestor by calling inheritFromWidgetOfExactType() in the widget's didChangeDependencies() method.

I guess it is too many levels of async magic, but not 100% sure on how to fix it.

Mattias Månsson

unread,
Jun 15, 2019, 9:11:30 PM6/15/19
to Flutter Development (flutter-dev)
Think I figured it out, I can't use pushReplacementNamed on the dialog as it would then be gone when I come back. If I change it to the following it works better:
Navigator.pushNamed(context, '/login').then((_) {
Navigator.of(context).pushReplacementNamed('/otherpage');
});

--
You received this message because you are subscribed to a topic in the Google Groups "Flutter Development (flutter-dev)" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/flutter-dev/4_gJ3alInr0/unsubscribe.
To unsubscribe from this group and all its topics, send an email to flutter-dev...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/flutter-dev/47e6f3d1-5bd4-4afb-adab-ddcd2e2d9463%40googlegroups.com.

Peter Kretschmer

unread,
Jun 20, 2019, 8:44:04 AM6/20/19
to Flutter Development (flutter-dev)
Did you Try

Navigator.popAndPushNamed(context, /route);
I hade a simular problem and this was the solution. So you basically go one step back first and then Push on Top. If you then push either the return button or try to go back by Navigator.pop it will nither crash nor go back to anything. But my loginflow was slightly different
To unsubscribe from this group and all its topics, send an email to flutt...@googlegroups.com.
Reply all
Reply to author
Forward
0 new messages