I need to know is there any way to perform a task during splash screen is visible in flutter. I know how to add splash screen in flutter but I don't know how to perform background operations during splash screen is visible. I can't find anything on the internet, please help.
Yes, yes you can. The main()
function can actually be tagged as async
, so you can do whatever you need to do before running runApp(...)
in the main()
method body, even asynchronously. This way, the splash screen will be shown until your asynchronous result is retrieved, before calling runApp(...)
. For example:
Future<void> main() async {
// Do whatever you need to do here
final home = await setHomeWidgetDependingOnLoginStatus();
return runApp(MyApp(home: home));
}
The animated_splash_screen package does this worderfully. add it to your 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,
)
This will load and hold you splash screen until your function is complete.
Your function has to return a widget, in this case is your home screen. This also allows for dynamic routing depending on your data eg, is user logged in.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With