Call future provider with delay on user progress in app

Viewed 55

my goal is to provide value of user location with a Provider in the whole app. It's important to me that is't on top of the app as I want to use the value in the routes also.

However, in my app user first needs to login. Only afterwards he gets to the map. Here is the thing. According to bussiness requirements I can't call for permissions before the user gets to the map widget. So the provider needs to be on top o the app but the future function has to be called once the user logs in. How can I achieve that?

Here is a sample of my MyApp widget.

    class MyApp extends StatelessWidget {
  // This widget is the root of your application.
  @override
  Widget build(BuildContext context) {
    return MultiProvider(
      providers: [
        StreamProvider<UserModel?>.value(
          initialData: null,
          value: AuthService().user,
        ),
        FutureProvider<LatLng?>.value(
          value: GeolocationService().getUserLocation(),
          initialData: null,
        )
      ],
      child: MaterialApp(
        title: '',
        theme: themeData(),
        onGenerateRoute: onGenerateRoute(),
        builder: EasyLoading.init(),
        home: AuthWrapper(),
      ),
    );
  }
}
1 Answers

SchedulerBinding.instances from scheduler. You can use it inside the initState of StatefulWidget

{
//inside initState method
SchedulerBinding.instances?.addPostframecallback((_){
  //anything run within this function will be called just after the very first build method
  // how it works?
  // before the build method ran, initState will be called first synchronously 
  // after that, build method will be called
  // then right after that build method finished the first render task,
  // the post frame callback will be called, in this place we can use context
  // since the UI has been built
});
}
Related