The app is open then After clicking notification main gets called again and then getting store object as null but still getting this error when await openStore() is called

Unhandled Exception: Bad state: failed to create store: 10001 Cannot open store: another store is still open using the same path
static Future<Store> getStore() async{
    if(store != null) {
      print("StoreIsNotNull");
      return store!;
    }else{
      print("StoreIsNull");
      store = await openStore();
      return store!;
    }
  }

So when notification is clicked then store object is getting as null.

4

There are 4 best solutions below

3
On

To avoid reopen store while it still already open, just use Completer :

import 'dart:async';

// global variable
final Completer<Store> _storeCompleter = Completer<Store>();

Future<void> initStore() async {
    if (!_storeCompleter.isCompleted) {
      final store = await openStore();
      _storeCompleter.complete(store); 
    }
}

Future<Store> getStore() async {
    return await _storeCompleter.future;
}

Future<void> main() async {
  // This is required so ObjectBox can get the application directory
  // to store the database in.
  WidgetsFlutterBinding.ensureInitialized();

  // initialize store
  await initStore();

  runApp(MyApp());
}

Then, if you want to get Person box from store :

final store = await getStore();
final box = store.box<Person>();
final person = Person(firstName: 'John', lastName: 'Doe');
box.put(person);

So when initStore is not complete, the getStore will wait and would not return any store instance.

0
On

Try this instead

use an async function, redo the code with an async function, if the function or command you wanted to do was done, it would move to next, instead of clicking on the notification that would become null and would stay there and it wouldn't close itself, that's kinda heavy for a program you should only allow executions it would have like : -> open me -> one done opening closed -> then done

0
On

Using objectbox: ^2.0.0

class ObjectBoxService {
  late final Store store;
  static ObjectBoxService? _instance;

  ObjectBoxService._create(this.store) {
    // Add any additional setup code, e.g. build queries.
  }

  static Future<ObjectBoxService> create() async {
    if (_instance != null) {
      return _instance!;
    } else {
      final docsDir = await getApplicationDocumentsDirectory();
      final storePath = p.join(docsDir.path, "hestatistics");

      // Check if the store is already open
      if (Store.isOpen(storePath)) {
        // Attach to the already opened store
        final store = Store.attach(getObjectBoxModel(),storePath);
        _instance = ObjectBoxService._create(store);
      } else {
        // Future<Store> openStore() {...} is defined in the generated objectbox.g.dart
        final store = await openStore(directory: storePath);
        _instance = ObjectBoxService._create(store);
      }
      return _instance!;
    }
  }
}
1
On

I encountered the same issue trying to open a store from a background task. The issue was resolved by using an already open store or create a new store if not yet opened.

if(Store.isOpen('directory-path')){
  // applicable when store is from other isolate
  return Store.attach(getObjectBoxModel(), 'directory-path');
}
return await openStore(directory: 'directory-path');