tizen_interop_callbacks
A Flutter plugin to resolve issues related to the error message: Cannot invoke native callback outside an isolate
.
This package is designed to be used with the tizen_interop
package.
Similarly to tizen_interop
, this package has no build time dependency on a specific Tizen profile or version.
Usage
-
Add this package and
tizen_interop
as dependencies in yourpubspec.yaml
file.dependencies: ffi: ^2.0.1 tizen_interop: ^0.4.1 tizen_interop_callbacks: ^0.3.1
-
In your Dart code, import the packages:
import 'dart:ffi'; import 'package:tizen_interop/6.0/tizen.dart'; import 'package:tizen_interop_callbacks/tizen_interop_callbacks.dart';
-
Instantiate the
TizenInteropCallbacks
class. This should be done in the root isolate - the thread where yourmain()
is called.final callbacks = TizenInteropCallbacks();
-
Implement your callback in Dart and register it with
TizenInteropCallbacks
:final callback = _callbacks.register<Void Function(Int32, Pointer<Void>, Pointer<Void>)>( 'device_changed_cb', Pointer.fromFunction(_batteryChanged), );
The native function type to be used in
register<>()
can be obtained by checking the definition of the related callback type - thedevice_changed_cb
in this case. -
Pass the obtained callback pointer and user_data to the Native API function:
Warning
Both
interopCallback
andinteropUserData
must be passed. The callback handling implementation relies on theinteropUserData
. The only exceptions are a few callbacks that do not acceptuser_data
parameters.final ret = tizen.device_add_callback( device_callback_e.DEVICE_CALLBACK_BATTERY_CAPACITY, callback.interopCallback, callback.interopUserData, );