我需要知道在Flutter中是否有办法在启动画面可见的时候执行任务。我知道如何在Flutter中添加启动画面,但是我不知道如何在启动画面可见的时候执行后台操作。我在互联网上找不到任何信息,请帮忙。
我需要知道在Flutter中是否有办法在启动画面可见的时候执行任务。我知道如何在Flutter中添加启动画面,但是我不知道如何在启动画面可见的时候执行后台操作。我在互联网上找不到任何信息,请帮忙。
是的,你可以。实际上,main()
函数可以被标记为 async
,因此你可以在 main()
方法体中做任何需要做的事情,甚至可以异步地进行。这样,在调用 runApp(...)
之前,启动屏幕将显示,直到异步结果被检索到。例如:
Future<void> main() async {
// Do whatever you need to do here
final home = await setHomeWidgetDependingOnLoginStatus();
return runApp(MyApp(home: home));
}
pubspec.yml
即可。 animated_splash_screen: ^1.1.0
AnimatedSplashScreen.withScreenFunction(
splash: 'images/splash.png',
screenFunction: () async{
// Your code here
return MainScreen();
},
splashTransition: SplashTransition.rotationTransition,
pageTransitionType: PageTransitionType.scale,
)