Home > Mobile >  Flutter Getx , Wait till all main binding load before Navigation
Flutter Getx , Wait till all main binding load before Navigation

Time:06-17

I am using getx on my project, I have a mainBianding page :

class MainBinding implements Bindings {
  @override
  Future<void> dependencies() async {
    Get.putAsync<HiveService>(() => HiveService().init(), permanent: true);
    Get.lazyPut<HomeController>(
      () => HomeController(

          dbclient: Get.find<HiveService>()),
    );
  }
}

I have a GETXService for initializing Hive

class HiveService extends GetxService {
  late Box<Model> vBox;

  Future<HiveService> init() async {
    final appDocumentDirectory =
        await path_provider.getApplicationDocumentsDirectory();
    Hive
      ..init(appDocumentDirectory.path)
      ..registerAdapter<Model>(ModelAdaptor())

    return this;
  }

After lunching App HomePage and HomeController will be launched but I got this error:

══╡ EXCEPTION CAUGHT BY WIDGETS LIBRARY ╞═══════════════════════════════════════════════════════════
The following message was thrown building Builder:
"HiveService" not found. You need to call "Get.put(HiveService())" or
"Get.lazyPut(()=>HiveService())"
The relevant error-causing widget was:
  ScrollConfiguration

because Hive service is a future Is has a delay to be loaded but I used Future<void> dependencies() async on this binding class. How do I have to wait to be sure HiveService load completely and after that Home Page load? I am using MainBinding Inside GetMaterialApp;

    Future main() async {
      await MainBinding().dependencies();
    ...

  runApp(MyApp()
    return GetMaterialApp(
      debugShowCheckedModeBanner: false,
      useInheritedMediaQuery: true,
      locale: DevicePreview.locale(context),
      theme: ThemeData(
        primarySwatch: Colors.blue,
      ),
      initialRoute: Routes.HOME,
      getPages: AppPages.pages,
      initialBinding: MainBinding(),

CodePudding user response:

test this

await Get.putAsync<HiveService>(() => HiveService().init(), permanent: true);
  • Related