获取错误为“未经手的异常:不良状态:无法创建商店:10001无法打开商店:另一家商店仍使用相同的路径打开。

发布于 2025-02-04 06:12:55 字数 552 浏览 4 评论 0原文

该应用是打开的,然后单击通知MAIN再次调用,然后获取store对象为null,但是当等待OpenStore()时仍会遇到此错误。被称为

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!;
    }
  }

,因此单击通知时,存储对象将作为null获取。

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

音栖息无 2025-02-11 06:12:55

我遇到了同样的问题,试图从背景任务开设商店。该问题是通过使用已经开放的商店或创建新商店(如果尚未打开的)来解决的。

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

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');
这个俗人 2025-02-11 06:12:55

要避免在仍在打开的情况下重新打开商店,只需使用完成器

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());
}

然后,如果您想从商店中获取Person box:

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

因此,当Initstore时未完成,getstore将等待,并且不会返回任何商店实例。

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.

南街九尾狐 2025-02-11 06:12:55

尝试

使用async函数,使用异步函数重做代码,如果您要执行的函数或命令是完成的,它将移至下一步不会关闭自己,对于一个程序来说,这有点沉重,您应该只允许执行它
喜欢: - &gt;打开我 - &gt;一个已完成的打开 - &GT;然后完成

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

请帮我爱他 2025-02-11 06:12:55

使用对象盒: ^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!;
    }
  }
}

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!;
    }
  }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文