Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Load Data during Splash Screen in Flutter

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.

like image 790
Taha Malik Avatar asked Oct 29 '19 21:10

Taha Malik


2 Answers

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));
}
like image 141
drogel Avatar answered Oct 06 '22 12:10

drogel


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.

like image 33
Antony Leons Avatar answered Oct 06 '22 12:10

Antony Leons