最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

flutter - ObjectBox Cannot open store: another store is still open using the same path - Stack Overflow

programmeradmin2浏览0评论

I have a flutter app that uses ObjectBox to cache some data for offline availability.

I have an issue where if I swipe back on my android phone and the app goes to the background, when I open the app again, it crashes with the following error:

StateError (Bad state: failed to create store: Cannot open store: another store is still open using the same path: "/data/data/com.....etc" (OBX_ERROR code 10001))

Now this only happens when I swipe back (from the side screen of my android) and open the app again.

If i just swipe up to put the app in background and open it again, it loads fine.

Below is my code on how i am using ObjectBox using Riverpod in my Flutter app:

main.dart

main() async {
  // Ensure the widgets are initialized
  final binding = WidgetsFlutterBinding.ensureInitialized();

  // Preserve the splash screen
  FlutterNativeSplash.preserve(widgetsBinding: binding);

  // Initialize the local DB
  final store = await openStore();

  // Remove splash now as the main content has been loaded
  FlutterNativeSplash.remove();

  // Run the main app
  runApp(
    TranslationProvider(
      child: ProviderScope(
        overrides: [
          objectBoxProvider.overrideWithValue(store),
        ],
        child: const MesMaterialApp(),
      ),
    ),
  );
}

local_database.dart

@riverpod
Store objectBox(Ref ref) {
  /*
  * Gets the object box provider
  */

  throw UnimplementedError('Initialize this in your main.dart');
}

@riverpod
MesServiceLocalDataSource mesServiceLocalDataSource(Ref ref) {
  /*
  * Gets the local data source for the MES service
  */

  // Get the object box provider
  final store = ref.watch(objectBoxProvider);

  // Return the local data source
  return MesServiceLocalDataSource(store);
}

mes_services.dart

class MesServiceLocalDataSource implements MesDataSource {
  final Store store;

  MesServiceLocalDataSource(this.store);

  @override
  Future<List<Service>> getAllServices(String lang) async {
    final box = store.box<ServiceModel>();
    final services =
        box.query(ServiceModel_.language.equals(lang)).build().find();

    return services.map((model) => model.toService()).toList();
  }

  Future<void> cacheServices(List<Service> services, String lang) async {
    final box = store.box<ServiceModel>();

    // Delete existing services for this language
    final existingServices =
        box.query(ServiceModel_.language.equals(lang)).build().find();
    box.removeMany(existingServices.map((e) => e.id).toList());

    // Store new services with downloaded images
    final serviceModels = await Future.wait(
        services.map((service) => ServiceModel.fromService(service, lang)));

    box.putMany(serviceModels);
  }
}

What can I try next?

发布评论

评论列表(0)

  1. 暂无评论