Flutter - How can I push to other Route without user interaction (like onTap)?

余生颓废 提交于 2019-12-11 08:46:13

问题


I am an iOS developer, and trying Flutter now.

As the title, what I want to achieve is to make a class act like "manager", to control my page route on app start.

For example in iOS, I can check a 'Bool' inside AppDelegate (didFinishedLaunchingWithOptions) / ViewController (ViewDidLoad/Appear) and change the rootViewController or push to a desire ViewController depends on the 'Bool', like something 'isLogin' to rather push to LoginViewController / LoggedViewController.

I know I can do Push on user interaction, like listening onTap of IconButton. But I have no idea how to do it 'Automatically'.

In my knowledge, 'build(context)' in Flutter is something similar to 'viewDidLoad' in iOS, but it is specifically for UI, so where can I put the logic?


回答1:


One way of Calling Navigator.push in InitState():

@override
  void initState() {
    super.initState();
    if(condition){
      WidgetsBinding.instance.addPostFrameCallback((_) async {
        Navigator.push(
            context,
            MaterialPageRoute(
                builder: (BuildContext context) => NextPage()
            )
        );
      });
    }
  }


来源:https://stackoverflow.com/questions/53038402/flutter-how-can-i-push-to-other-route-without-user-interaction-like-ontap

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!