|
1 | 1 | import 'dart:ffi';
|
2 | 2 | import 'dart:io' show Platform;
|
3 | 3 |
|
| 4 | +import '../common.dart'; |
| 5 | +import 'helpers.dart'; |
4 | 6 | import 'objectbox-c.dart';
|
5 | 7 |
|
6 | 8 | // let files importing bindings.dart also get all the OBX_* types
|
@@ -44,14 +46,27 @@ ObjectBoxC /*?*/ _cachedBindings;
|
44 | 46 |
|
45 | 47 | ObjectBoxC get C => _cachedBindings ??= loadObjectBoxLib();
|
46 | 48 |
|
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 |
50 | 54 | 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; |
54 | 65 | }
|
55 | 66 | }
|
56 | 67 |
|
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; |
0 commit comments