Flutter Getx ,等待所有主绑定加载后再导航

say*_*bir 1 flutter flutter-getx

我在我的项目中使用 getx,我有一个 mainBianding 页面:

\n
class MainBinding implements Bindings {\n  @override\n  Future<void> dependencies() async {\n    Get.putAsync<HiveService>(() => HiveService().init(), permanent: true);\n    Get.lazyPut<HomeController>(\n      () => HomeController(\n\n          dbclient: Get.find<HiveService>()),\n    );\n  }\n}\n
Run Code Online (Sandbox Code Playgroud)\n

我有一个用于初始化 Hive 的 GETXService

\n
class HiveService extends GetxService {\n  late Box<Model> vBox;\n\n  Future<HiveService> init() async {\n    final appDocumentDirectory =\n        await path_provider.getApplicationDocumentsDirectory();\n    Hive\n      ..init(appDocumentDirectory.path)\n      ..registerAdapter<Model>(ModelAdaptor())\n\n    return this;\n  }\n
Run Code Online (Sandbox Code Playgroud)\n

午餐后,App HomePage 和 HomeController 将启动,但我收到此错误:

\n
\xe2\x95\x90\xe2\x95\x90\xe2\x95\xa1 EXCEPTION CAUGHT BY WIDGETS LIBRARY \xe2\x95\x9e\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\nThe following message was thrown building Builder:\n"HiveService" not found. You need to call "Get.put(HiveService())" or\n"Get.lazyPut(()=>HiveService())"\nThe relevant error-causing widget was:\n  ScrollConfiguration\n
Run Code Online (Sandbox Code Playgroud)\n

因为 Hive 服务是一个 future ,它有一个延迟加载,但我Future<void> dependencies() async在这个绑定类上使用了。我必须如何等待才能确保 HiveService 完全加载并在主页加载之后?\n我正在使用 MainBinding Inside GetMaterialApp

\n
    Future main() async {\n      await MainBinding().dependencies();\n    ...\n\n  runApp(MyApp()\n    return GetMaterialApp(\n      debugShowCheckedModeBanner: false,\n      useInheritedMediaQuery: true,\n      locale: DevicePreview.locale(context),\n      theme: ThemeData(\n        primarySwatch: Colors.blue,\n      ),\n      initialRoute: Routes.HOME,\n      getPages: AppPages.pages,\n      initialBinding: MainBinding(),\n
Run Code Online (Sandbox Code Playgroud)\n

Hos*_*adi 7

测试这个

await Get.putAsync<HiveService>(() => HiveService().init(), permanent: true);
Run Code Online (Sandbox Code Playgroud)