databasefluttersqliteflutter-floorget-it

Flutter get_it factory not ready yet [Flutter get_it library with Floor database initialization]


I'm trying to use floor library with get_it library to simplify call

simple implementation of this library is:

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

in main() function i call:

await sl.init();

but i get error:

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'

What's wrong in my code ?? Thank you

i refered answer to this https://stackoverflow.com/questions/56497896/make-a-simple-single-instanse-class-as-database-helper


Solution

  • use dependsOn refer to https://pub.dev/packages/get_it#automatic-using-dependson and chaining that its each module.

    e.g:

    getIt.registerSingletonAsync<AppDatabase>(() async => $FloorAppDatabase
          .databaseBuilder('app_database.db')
          .addCallback(callback)
          .build());
    
    getIt.registerSingletonWithDependencies<UserDao>(
          () => getIt<AppDatabase>().userDao,
          dependsOn: [AppDatabase]);