KTang
KTang

Reputation: 350

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

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 the 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?

Upvotes: 1

Views: 1559

Answers (1)

anmol.majhail
anmol.majhail

Reputation: 51186

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()
            )
        );
      });
    }
  }

Upvotes: 4

Related Questions