[Hydrated Bloc] Synchronize across isolates (Use bloc data in background isolates)
- Dominant language
- Dart
- Stars
- 12.5k
- Forks
- 3.4k
- PR merge metrics
- No merged PRs in 30d
Description
**Is your feature request related to a problem? Please describe.**
I'm trying to share state between my main isolate and a background callback (isolate). Currently, the instance(s) have different state.
**Describe the solution you'd like**
Either of the following:
1. The cubit/bloc state should be usable across isolates. (Seems challenging, but I'm new to Flutter/Dart)
2. A manual means of "refreshing" a hydrated bloc to re-load from storage. `reloadFromStorage()`
**Describe alternatives you've considered**
1. Using shared preferences
2. Trying to use Hive directly
3. Using Hydrated Bloc: Restarting app, causing the initial load of data to pull the new values. Needed on every switch between updating a value via UI, and a background callback having the updated value.
**Additional context**
In both main and a background isolate callback, I ensure that I initialize storage as follows. Each isolate then creates an instance of my `SettingsCubit` which contains state I'd like to share between my UI and a background isolate (long running timers). Sharing a single `final settings = SettingsCubit()` instance yields the same results; state is not kept in sync across isolates.
```
initStorage() async {
try {
HydratedBloc.storage;
debugPrint(
'[${DateTime.now()}] Isolate: ${Isolate.current.hashCode} bloc storage: initialized already');
} catch (error) {
debugPrint(
'[${DateTime.now()}] Isolate: ${Isolate.current.hashCode} bloc storage: initializing');
HydratedBloc.storage = await HydratedStorage.build(
storageDirectory: kIsWeb
? HydratedStorage.webStorageDirectory
: await getTemporaryDirectory(),
);
}
}
```
Contributor guide
Assessment
This issue has not been assessed yet.