0

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
  • you should init() the box only once per app session. That means in a method that runs only once per lifecycle, like main(). I hope that’s the invoking method as i see no code pertaining the call to init(). Commented Sep 14, 2023 at 17:24

1 Answer 1

0

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
}
Sign up to request clarification or add additional context in comments.

1 Comment

Remember that Stack Overflow isn't just intended to solve the immediate problem, but also to help future readers find solutions to similar problems, which requires understanding the underlying code. This is especially important for members of our community who are beginners, and not familiar with the syntax. Given that, can you edit your answer to include an explanation of what you're doing and why you believe it is the best approach?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.