Ccr
Ccr

Reputation: 686

Flutter: Navigator within Promise not working

I am trying to navigate to next screen after successfully logged in from facebook.

My main.dart looks like this.

   void main() => runApp(new MaterialApp(
  theme: new ThemeData(primarySwatch: Colors.brown),
  home: new Login(),
  routes: <String, WidgetBuilder> {
    "/rooms" : (BuildContext context) => new RoomScreen(),
    "/room" : (BuildContext context) => new RoomScreen(),
  },
));

My Login looks like this,

     class _Login extends State<Login> {
       ...
      _showRoom() => Navigator.pushNamed(context, "/room");

      _initiateLogin()  =>
          _facebookConnect.login(storeToken: false, cookie: false).then(
              (token) {
                if(token != Null) {
                  debugPrint("showing rooms");
                  _showRoom();
                }
              }
          );

      @override
      Widget build(BuildContext context) =>
          new Scaffold(
            appBar: new AppBar(title: new Text("Hello"),),
            body: new ListTile(
              onTap: () { _initiateLogin() },
                 .....
              ),
            ),
          );
    }

when I add _showRoom() in onTap: it works, and the screen changes. but when I initiate login and then push, it does not work.

onTap: () {setState(() { _showRoom(); });}, => this works

onTap: () {setState(() { _initiateLogin(); });}, => but this does not work. Even though I can see "showing rooms" on the console. I can also see that token is present too. but It is still in the login page. Plus the login page is freezed. I cannot tap on button any more after that.

Am I missing something here?

Upvotes: 0

Views: 1506

Answers (1)

Ccr
Ccr

Reputation: 686

The problem was, I was trying to navigate to another screen before the modal or the pop up for facebook login view was dismissed. The promise would be called write away, but it takes few miliseconds for that loging session view to dismiss.

await new Future.delayed(new Duration(milliseconds: 500));
 Navigator.of(context).pushNamed("/rooms");

This did the trick. I delayed the Navigation push for 500 milisecond to ensure that the fb login session view was dissmissed.

Upvotes: 1

Related Questions