扑来getx,等到所有主要绑定负载之前,才导航

发布于 2025-02-07 20:37:19 字数 1755 浏览 2 评论 0原文

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

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

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

我有一个getXservice

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;
  }

午餐后将午餐后初始化Hive和Homecontroller启动此错误:

══╡ 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

因为Hive Service是未来的,但要加载延迟,但要延迟我使用了Future&lt; void&gt;此绑定类上的依赖性()async。我必须等待如何确定赫维斯服务的负载,并在本主页加载之后进行全部负载? 我在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(),

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(),

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

南…巷孤猫 2025-02-14 20:37:19

测试这个

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

test this

await Get.putAsync<HiveService>(() => HiveService().init(), permanent: true);
谁人与我共长歌 2025-02-14 20:37:19

您需要首先运行service.init()函数。然后返回GetX服务(Hiveservice),然后完成以将此服务加载到内存。

  @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>()),
    );
  }
}

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>()),
    );
  }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文