
Integrates Objectbox database initialization into the main application startup process. Adds `path` and `path_provider` as direct dependencies, which are typically required for Objectbox to locate the database directory.
35 lines
1002 B
Dart
35 lines
1002 B
Dart
import 'dart:io';
|
|
import 'package:path/path.dart' as p;
|
|
import 'package:path_provider/path_provider.dart';
|
|
|
|
import 'package:barcode_scanner/backend/objectbox/objectbox.g.dart';
|
|
|
|
late ObjectboxManager objectboxManager;
|
|
|
|
class ObjectboxManager {
|
|
/// The Store of this app.
|
|
late final Store store;
|
|
|
|
ObjectboxManager._create(this.store);
|
|
|
|
/// Create an instance of ObjectBox to use throughout the app.
|
|
static Future<ObjectboxManager> create() async {
|
|
Directory directory;
|
|
if (Platform.isIOS) {
|
|
directory = await getLibraryDirectory();
|
|
} else {
|
|
directory = await getApplicationSupportDirectory();
|
|
}
|
|
// Future<Store> openStore() {...} is defined in the generated objectbox.g.dart
|
|
final store = await openStore(
|
|
directory: p.join(directory.path, "objectbox_db"),
|
|
);
|
|
return ObjectboxManager._create(store);
|
|
}
|
|
|
|
/// Call it before `runApp()` in main.dart
|
|
static Future init() async {
|
|
objectboxManager = await ObjectboxManager.create();
|
|
}
|
|
}
|