I'm trying to store data locally with ObjectBox in Flutter, and I started getting this error. Everything looks good according to the documentation, I'm unsure why I'm getting errors from the objectbox.g.dart file: StateError (Bad state: failed to create store: Cannot open store: another store is still open using the same path: "/data/data/com.example.my_app/app_flutter/objectbox" (OBX_ERROR code 10001)).

// initializations
late ObjectBox objectBox;

Future main() async {
  WidgetsFlutterBinding.ensureInitialized();
  objectBox = await ObjectBox.init();

  runApp(const MyApp());
}
// main code
// object_box.dart
class ObjectBox {
  late final Store _store;
  late final Box<RoutineDB> _routineBox;
  late final Box<ExerciseDB> _exerciseBox;

  ObjectBox._init(this._store) {
    _routineBox = Box<RoutineDB>(_store);
    _exerciseBox = Box<ExerciseDB>(_store);
  }
  static Future<ObjectBox> init() async {
    final dir = await getApplicationDocumentsDirectory();
    final store = await openStore(directory: p.join(dir.path, "objectbox"));
    return ObjectBox._init(store);
  }
// other functions
}

The error appears in the "openStore" function within the objectbox.g.dart file.

Future<Store> openStore(
        {String? directory,
        int? maxDBSizeInKB,
        int? fileMode,
        int? maxReaders,
        bool queriesCaseSensitiveDefault = true,
        String? macosApplicationGroup}) async =>
    Store(getObjectBoxModel(), // getting error here
        directory: directory ?? (await defaultStoreDirectory()).path,
        maxDBSizeInKB: maxDBSizeInKB,
        fileMode: fileMode,
        maxReaders: maxReaders,
        queriesCaseSensitiveDefault: queriesCaseSensitiveDefault,
        macosApplicationGroup: macosApplicationGroup);
1

There are 1 best solutions below

1
Ashton Valeroso On

I figured it out. This is the best solution that I was able to come up with. Use a try-catch handler when opening the store. If an exception occurs, the store will be attached instead of opened. I haven't encountered the same error since I implemented this solution. Hope this helps.

class ObjectBox {
  late final Store _store;
  late final Box<RoutineDB> _routineBox;
  late final Box<ExerciseDB> _exerciseBox;
  static ObjectBox? _instance;

  ObjectBox._init(this._store) {
    _routineBox = Box<RoutineDB>(_store);
    _exerciseBox = Box<ExerciseDB>(_store);
  }
  static Future<ObjectBox> init() async {
    if (_instance != null) {
      return _instance!;
    } else {
      final dir = await getApplicationDocumentsDirectory();
      final storePath = p.join(dir.path, "objectbox");

      // Check if the store is already open
      try {
        // Future<Store> openStore() {...} is defined in the generated objectbox.g.dart
        final store = await openStore(directory: storePath);
        _instance = ObjectBox._init(store);
      } catch (e) {
        final store = Store.attach(getObjectBoxModel(), storePath);
        _instance = ObjectBox._init(store);
      }
      return _instance!;
    }
  }
// other code
}