isar / isar/hive

How to run Hive in an isolate?

Open
#433 11 comments 14 reactions 0 assignees View on GitHub
question
Dominant language
Dart
Stars
4.4k
Forks
449
PR merge metrics
No merged PRs in 30d

Description

**Question**
Hi, I'm new to Hive and I'm trying to integrate it into my project. I've managed to generate a large static database that performs really well (much better than sqlite), and it works perfectly when I'm running the part of the project that uses it alone outside of flutter.
However, this part runs in an isolate because otherwise it freezes the UI, and the database has to be inside this part because otherwise the structure of the code would be horrific and unmaintainable, and this presents a problem.

When I try to run hive in the isolate I run into this error:

```
E/flutter (17531): [ERROR:flutter/runtime/dart_isolate.cc(865)] Unhandled exception:
E/flutter (17531): ServicesBinding.defaultBinaryMessenger was accessed before the binding was initialized.
E/flutter (17531): If you're running an application and need to access the binary messenger before `runApp()` has been called (for example, during plugin initialization), then you need to explicitly call the `WidgetsFlutterBinding.ensureInitialized()` first.
E/flutter (17531): If you're running a test, you can call the `TestWidgetsFlutterBinding.ensureInitialized()` as the first line in your test's `main()` method to initialize the binding.
```

Of course I have _WidgetsFlutterBinding.ensureInitialized()_ in main() before runApp(), and I cannot put it in the isolate because I get this:
```E/flutter (17181): UI actions are only available on root isolate.```

So far I've tried:

- Hive.init() inside the isolate
- Hive.init() in main
- Hive.initFlutter() in the isolate (can't do UI things outside the main isolate)
- Hive.initFlutter() in main

I get the same error whatever I do. I've seen some people here talking about running Hive in an isolate here so I know it's possible, I just don't know what I'm missing here.

**Version**
- Platform: Android / iOS
- Flutter version: 1.20.2
- Hive version: 1.4.4

Thanks in advance

Contributor guide

No contributing guide indexed for this repository

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.