【问题标题】:Flutter get_it factory not ready yet [Flutter get_it library with Floor database initialization]Flutter get_it 工厂尚未准备好 [Flutter get_it 库与 Floor 数据库初始化]
【发布时间】:2023-01-27 14:34:08
【问题描述】:

我正在尝试将 floor 库与 get_it 库一起使用来简化调用

这个库的简单实现是:

final sl = GetIt.instance;

Future<void> init() async {
  /// database
  sl.registerLazySingletonAsync<AppDatabase>(
      () => $FloorAppDatabase.databaseBuilder('app_database.db').build());

  /// http client
  sl.registerLazySingleton<ApiService>(
      () => NetworkModule().getHttpProvider().getService());

  /// call dao
  sl.registerLazySingletonAsync<UserDao>(
      () async => (await sl.getAsync<AppDatabase>()).userDao);

在 main() 函数中我调用:

等待 sl.init();

但我得到错误:

You tried to access an instance of UserDao that is not ready yet
'package:get_it/get_it_impl.dart':
package:get_it/get_it_impl.dart:1
Failed assertion: line 404 pos 9: 'instanceFactory.isReady'

我的代码有什么问题?? 谢谢

我提到了这个答案 https://stackoverflow.com/questions/56497896/make-a-simple-single-instanse-class-as-database-helper

【问题讨论】:

  • 同样在这里。你找到什么了吗?

标签: database flutter sqlite flutter-floor get-it


【解决方案1】:

使用 dependsOn 引用 https://pub.dev/packages/get_it#automatic-using-dependson 并链接它的每个模块。

例如:

getIt.registerSingletonAsync<AppDatabase>(() async => $FloorAppDatabase
      .databaseBuilder('app_database.db')
      .addCallback(callback)
      .build());

getIt.registerSingletonWithDependencies<UserDao>(
      () => getIt<AppDatabase>().userDao,
      dependsOn: [AppDatabase]);

【讨论】:

    猜你喜欢
    • 2021-12-06
    • 2022-06-19
    • 2021-10-15
    • 2020-10-16
    • 1970-01-01
    • 1970-01-01
    • 2020-09-03
    • 2021-11-30
    • 2021-08-03
    相关资源
    最近更新 更多