Skip to content

Commit 5ba2d07

Browse files
committed
Dart native API initialization error handling
1 parent 2c81509 commit 5ba2d07

File tree

2 files changed

+23
-8
lines changed

2 files changed

+23
-8
lines changed

objectbox/lib/src/bindings/bindings.dart

+22-7
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
import 'dart:ffi';
22
import 'dart:io' show Platform;
33

4+
import '../common.dart';
5+
import 'helpers.dart';
46
import 'objectbox-c.dart';
57

68
// let files importing bindings.dart also get all the OBX_* types
@@ -44,14 +46,27 @@ ObjectBoxC /*?*/ _cachedBindings;
4446

4547
ObjectBoxC get C => _cachedBindings ??= loadObjectBoxLib();
4648

47-
/// Init DartAPI in C for async callbacks - only needs to be called once.
48-
/// See the following issue:
49-
/// https://github.com/objectbox/objectbox-dart/issues/143
49+
/// Init DartAPI in C for async callbacks.
50+
///
51+
/// Call each time you're assign a native listener - will throw if the Dart
52+
/// native API isn't available.
53+
/// See https://github.com/objectbox/objectbox-dart/issues/143
5054
void initializeDartAPI() {
51-
if (!_dartAPIinitialized) {
52-
_dartAPIinitialized = true;
53-
C.dart_init_api(NativeApi.initializeApiDLData);
55+
if (_dartAPIinitialized == null) {
56+
final errCode = C.dart_init_api(NativeApi.initializeApiDLData);
57+
_dartAPIinitialized = (OBX_SUCCESS == errCode);
58+
if (!_dartAPIinitialized) {
59+
_dartAPIinitException = latestNativeError(codeIfMissing: errCode);
60+
}
61+
}
62+
63+
if (_dartAPIinitException != null) {
64+
throw _dartAPIinitException;
5465
}
5566
}
5667

57-
bool _dartAPIinitialized = false;
68+
// null => not initialized
69+
// true => initialized successfully
70+
// false => failed to initialize - incompatible Dart version
71+
bool /*?*/ _dartAPIinitialized;
72+
ObjectBoxException /*?*/ _dartAPIinitException;

objectbox/lib/src/sync.dart

+1-1
Original file line numberDiff line numberDiff line change
@@ -490,7 +490,7 @@ class _SyncListenerGroup<StreamValueType> {
490490
}
491491

492492
void _debugLog(String message) {
493-
print('Listener ${name}: $message');
493+
// print('Listener ${name}: $message');
494494
}
495495
}
496496

0 commit comments

Comments
 (0)