1

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(),
Cyrus the Great
  • 5,145
  • 5
  • 68
  • 149
  • 1
    You are not awaiting `Get.putAsync(...)`, try `await Get.putAsync(...)`. Without the `await` keyword, the future is created and the operation is run, but the code continues to execute before the result is returned. – offworldwelcome Jun 16 '22 at 16:58
  • Here it the complete example of nested controllers: https://stackoverflow.com/a/75497500/16194683 – Muhammad Anees Feb 20 '23 at 10:37
  • Here is the complete example of nested controllers https://stackoverflow.com/a/75497500/16194683 – Muhammad Anees Feb 20 '23 at 10:41
  • Here is the complete example of nested controllers: https://stackoverflow.com/a/75497500/16194683 – Muhammad Anees Feb 20 '23 at 10:44

2 Answers2

6

test this

await Get.putAsync<HiveService>(() => HiveService().init(), permanent: true);
Hossein Asadi
  • 768
  • 1
  • 6
  • 15
1

You need to run your service.init() function first. and then return the getx Service(HiveService) and complete to load this service to the memory.

  @override
  Future<void> dependencies() async {
    await Get.putAsync< HiveService >(() async {
      final HiveService service = HiveService();
      await service.init();
      return service;
    });
    Get.lazyPut<HomeController>(
      () => HomeController(

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