Repository navigation
When using flutter_background_service Store can not be opened from a background service #699
Description
Activity
I just recently commented about this: #696 (comment)
Note: I labeled this issue with "more info required" so it will auto-close in a few days if there are no follow-up comments.
- addedquestionHow to do something/general questionHow to do something/general questionmore info requiredNeeds more info to become actionable. Auto-closed if no response.Needs more info to become actionable. Auto-closed if no response.and removedbugSomething isn't workingSomething isn't working
on Dec 9, 2024 Tried using attach method inside a background service onStart method and it says,
ObjectBoxException` (ObjectBoxException: could not attach to the store at given path - please ensure it was opened before) store createdand If I try to open the store again, It gives can not open store error which I mentioned above.
Here is my code for open and attach store functions:Future<Store> createStore() async { final docsDir = await getApplicationDocumentsDirectory(); // Future<Store> openStore() {...} is defined in the generated objectbox.g.dart final st = openStore(directory: p.join(docsDir.path, "obx-example")); return st; } Future<Store> attachStore() async { // final storeRef = globalStore!.reference; final docsDir = await getApplicationDocumentsDirectory(); return Store.attach(getObjectBoxModel(), p.join(docsDir.path, "obx-example") ); }
I called this create store method in main method and attach store method inside service onStart method.
- removedmore info requiredNeeds more info to become actionable. Auto-closed if no response.Needs more info to become actionable. Auto-closed if no response.
on Dec 10, 2024 Thanks for the details! As it looks like Dart state is shared, maybe I jumped to conclusions and this background service does not actually run in a separate isolate, but the main isolate.
In that case, have you tried to make your code access the Store from a global variable and only initialize it in the main method, like in the examples?
- addedmore info requiredNeeds more info to become actionable. Auto-closed if no response.Needs more info to become actionable. Auto-closed if no response.
on Dec 10, 2024 Yes, I have tried using a global variable. assign store to that variable in main method and when access that variable inside service onStart, variable have null value.
I can share the complete code in case you need it?
- removedmore info requiredNeeds more info to become actionable. Auto-closed if no response.Needs more info to become actionable. Auto-closed if no response.
on Dec 11, 2024 @Chandio763 Yes, a working code example is always more helpful! Anyhow, I have to look into how this background service package actually works to understand how to fix this.
- addedmore info requiredNeeds more info to become actionable. Auto-closed if no response.Needs more info to become actionable. Auto-closed if no response.
on Dec 11, 2024 Without additional information, we are unfortunately not sure how to resolve this issue. Therefore this issue has been automatically closed. Feel free to comment with additional details and we can re-open this issue.
Reopening: while a code example is missing, it appears it is still useful to look into this use case.
- removedmore info requiredNeeds more info to become actionable. Auto-closed if no response.Needs more info to become actionable. Auto-closed if no response.
on Jan 14, 2025 - changed the title
[-]Store can not be opened from a background service when initialized from main thread, neither I am able to check if the store is open on given location.[/-][+]When using flutter_background_service Store can not be opened from a background service[/+]on Sep 9, 2025
I have searched existing issues
Steps to reproduce
### Expected behavior
If I am inside the background service. I should be able to openStore on that location to access data or I can get that opened store to perform operations. I should be able to perform operations on same store from UI thread and background service as well.
Actual behavior:
It throws error stating store is already opened.
Code
Please use example code of flutter background service
Things you maybe should also include:
My pubspec.yaml
Logs