@@ -219,12 +219,7 @@ class Store implements Finalizable {
219
219
ArgumentError .value (macosApplicationGroup, 'macosApplicationGroup' ,
220
220
'Must be at most 20 characters long' );
221
221
}
222
- final cStr = macosApplicationGroup.toNativeUtf8 ();
223
- try {
224
- C .posix_sem_prefix_set (cStr.cast ());
225
- } finally {
226
- malloc.free (cStr);
227
- }
222
+ withNativeString (macosApplicationGroup, C .posix_sem_prefix_set);
228
223
}
229
224
_checkStoreDirectoryNotOpen ();
230
225
final model = Model (modelDefinition.model);
@@ -235,12 +230,8 @@ class Store implements Finalizable {
235
230
236
231
try {
237
232
checkObx (C .opt_model (opt, model.ptr));
238
- final cStr = safeDirectoryPath.toNativeUtf8 ();
239
- try {
240
- checkObx (C .opt_directory (opt, cStr.cast ()));
241
- } finally {
242
- malloc.free (cStr);
243
- }
233
+ checkObx (withNativeString (
234
+ safeDirectoryPath, (cStr) => C .opt_directory (opt, cStr)));
244
235
if (maxDBSizeInKB != null && maxDBSizeInKB > 0 ) {
245
236
C .opt_max_db_size_in_kb (opt, maxDBSizeInKB);
246
237
}
@@ -389,16 +380,13 @@ class Store implements Finalizable {
389
380
_checkStoreDirectoryNotOpen ();
390
381
391
382
final safeDirectoryPath = _safeDirectoryPath (directoryPath);
392
- final pathCStr = safeDirectoryPath.toNativeUtf8 ();
393
- try {
383
+ withNativeString (safeDirectoryPath, (cStr) {
394
384
if (debugLogs) {
395
- final isOpen = C .store_is_open (pathCStr. cast () );
385
+ final isOpen = C .store_is_open (cStr );
396
386
print ('Attaching to store... path=$safeDirectoryPath isOpen=$isOpen ' );
397
387
}
398
- _cStore = C .store_attach (pathCStr.cast ());
399
- } finally {
400
- malloc.free (pathCStr);
401
- }
388
+ _cStore = C .store_attach (cStr);
389
+ });
402
390
403
391
checkObxPtr (_cStore,
404
392
'could not attach to the store at given path - please ensure it was opened before' );
@@ -496,12 +484,24 @@ class Store implements Finalizable {
496
484
/// For Dart Native apps, pass null to use the [defaultDirectoryPath] .
497
485
static bool isOpen (String ? directoryPath) {
498
486
final path = _safeDirectoryPath (directoryPath);
499
- final cStr = path.toNativeUtf8 ();
500
- try {
501
- return C .store_is_open (cStr.cast ());
502
- } finally {
503
- malloc.free (cStr);
504
- }
487
+ return withNativeString (path, C .store_is_open);
488
+ }
489
+
490
+ /// Returns the file size in bytes of the main database file for the given
491
+ /// [directoryPath] , or 0 if the file does not exist or some error occurred.
492
+ ///
493
+ /// For in-memory databases, it is supported to pass the [inMemoryPrefix] and
494
+ /// the identifier. The rough size in bytes of the in-memory database will be
495
+ /// reported instead.
496
+ ///
497
+ /// For Flutter apps, the default [directoryPath] can be obtained with
498
+ /// `(await defaultStoreDirectory()).path` from `objectbox_flutter_libs`
499
+ /// (or `objectbox_sync_flutter_libs` ).
500
+ ///
501
+ /// For Dart Native apps, pass null to use the [defaultDirectoryPath] .
502
+ static int dbFileSize (String ? directoryPath) {
503
+ final path = _safeDirectoryPath (directoryPath);
504
+ return withNativeString (path, C .db_file_size);
505
505
}
506
506
507
507
/// Danger zone! This will delete all files in the given directory!
@@ -519,12 +519,7 @@ class Store implements Finalizable {
519
519
/// For Dart Native apps, pass null to use the [defaultDirectoryPath] .
520
520
static void removeDbFiles (String ? directoryPath) {
521
521
final path = _safeDirectoryPath (directoryPath);
522
- final cStr = path.toNativeUtf8 ();
523
- try {
524
- checkObx (C .remove_db_files (cStr.cast ()));
525
- } finally {
526
- malloc.free (cStr);
527
- }
522
+ checkObx (withNativeString (path, C .remove_db_files));
528
523
}
529
524
530
525
/// Returns a store reference you can use to create a new store instance with
0 commit comments