Skip to content

How to use with floor library? #99

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
jackyhieu1211-hn opened this issue Jul 31, 2020 · 3 comments
Closed

How to use with floor library? #99

jackyhieu1211-hn opened this issue Jul 31, 2020 · 3 comments

Comments

@jackyhieu1211-hn
Copy link

jackyhieu1211-hn commented Jul 31, 2020

Hello

@Database(version: 1, entities: [Person], views: [Name])
abstract class AppDatabase extends FloorDatabase {
  PersonDao get person;
}

Use in main.dart

var database = await $FloorAppDatabase.databaseBuilder('test.db').build();
database.person.getAllPerson();

===> OK

In get_id. I use like below

   static Future<void> _registerDatabase() async {
    AppDatabase database =
        await $FloorAppDatabase.databaseBuilder('test.db').build();
    sl.registerLazySingletonAsync(() async => database.person);
  }

in main.dart

I use like below

class _MainAppState extends BaseState<MainApp> {
  Person person = sl.get();

  @override
  Widget body() {
    get();
    return Container();
  }

  void get() async {
    print("DATABASE =========> ${person.getAllPerson()}");
  }
}

And get error

You tried to access an instance of Person that was not ready yet
'package:get_it/get_it_impl.dart':
Failed assertion: line 272 pos 14: 'instanceFactory.isReady'

Please help me. thank you very much.

@escamoteur
Copy link
Collaborator

you don't have to register the person in GetIt but the database.

Instead of

   static Future<void> _registerDatabase() async {
    AppDatabase database =
        await $FloorAppDatabase.databaseBuilder('test.db').build();
    sl.registerLazySingletonAsync(() async => database.person);
  }

do something like:

   static Future<void> _registerDatabase() async {
    sl.registerLazySingletonAsync<AppDatabase>($FloorAppDatabase.databaseBuilder('test.db').build());
  }
```

Then before you access it wait with 

```
sl.allReady()
```
after that you can access person over

```
sl<AppDataBase>().person;
```

@jackyhieu1211-hn
Copy link
Author

I close issue. And remove it to #101

@luangs7
Copy link

luangs7 commented Jan 27, 2023

using 7.2.0, and floor 1.3.0, database never get ready.

 registerLazySingletonAsync<BookDatabase>(() async =>
        await $FloorBookDatabase.databaseBuilder("app_database.db").build());
    registerSingletonWithDependencies<BookDao>(
        () => get<BookDatabase>().bookDao,
        dependsOn: [BookDatabase],
        signalsReady: true);

at this point, line 1 never execute no matter what. I've waited so long... Any ideas of whats going on?

return FutureBuilder(
        future: GetIt.instance.allReady(),
        builder: (BuildContext context, AsyncSnapshot snapshot) {
          if (snapshot.hasData) {
            //line 1
          } else {
            //line 2
          }
        });


Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants