问题
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