Please Help Me.
My Flutter Plugin Java can't return the list from native android please tell me to resolve it.
I created a flutter plugin to connect to my watch. I created a method Chanel to execute android native SDK, but I am stuck because I can't send a value back to flutter in this function but other part works fine. Disturbing all of you help me fix this issue too.
this error
W/System.err(21058): java.lang.RuntimeException: Methods marked with @UiThread must be executed on the main thread. Current thread: Binder:21058_2
W/System.err(21058): at io.flutter.embedding.engine.FlutterJNI.ensureRunningOnMainThread(FlutterJNI.java:1280)
W/System.err(21058): at io.flutter.embedding.engine.FlutterJNI.dispatchPlatformMessage(FlutterJNI.java:909)
W/System.err(21058): at io.flutter.embedding.engine.dart.DartMessenger.send(DartMessenger.java:72)
W/System.err(21058): at io.flutter.embedding.engine.dart.DartExecutor$DefaultBinaryMessenger.send(DartExecutor.java:384)
W/System.err(21058): at io.flutter.embedding.engine.dart.DartExecutor.send(DartExecutor.java:176)
W/System.err(21058): at io.flutter.plugin.common.EventChannel$IncomingStreamRequestHandler$EventSinkImplementation.success(EventChannel.java:221)
W/System.err(21058): at com.achatsocial.releep_watch_connect.ReleepWatchConnectPlugin$6.onDataResponse(ReleepWatchConnectPlugin.java:259)
W/System.err(21058): at a.b.f(Unknown Source:187)
W/System.err(21058): at a.b.b(Unknown Source:95)
W/System.err(21058): at b.a$a.onCharacteristicChanged(Unknown Source:82)
W/System.err(21058): at android.bluetooth.BluetoothGatt$1$8.run(BluetoothGatt.java:478)
W/System.err(21058): at android.bluetooth.BluetoothGatt.runOrQueueCallback(BluetoothGatt.java:780)
W/System.err(21058): at android.bluetooth.BluetoothGatt.access$200(BluetoothGatt.java:41)
W/System.err(21058): at android.bluetooth.BluetoothGatt$1.onNotify(BluetoothGatt.java:472)
W/System.err(21058): at android.bluetooth.IBluetoothGattCallback$Stub.onTransact(IBluetoothGattCallback.java:306)
W/System.err(21058): at android.os.Binder.execTransactInternal(Binder.java:1159)
W/System.err(21058): at android.os.Binder.execTransact(Binder.java:1123)
Example My Code Method channel
@Override
public void onMethodCall(@NonNull MethodCall call, @NonNull Result result) {
if (call.method.equals("syncHealthData")) {
int dataType = call.argument("dataType");
// ArrayList healthDataList =
// syncHealthDataByDataType(Constants.DATATYPE.Health_HistoryHeart);
ArrayList lists = new ArrayList();
YCBTClient.healthHistoryData(Constants.DATATYPE.Health_HistoryHeart, new BleDataResponse() {
@Override
public void onDataResponse(int i, float v, HashMap hashMap) {
if (hashMap != null) {
lists.addAll((ArrayList) hashMap.get("data"));
android.util.Log.e("history", "hashMap=" hashMap.toString());
result.success(lists);
} else {
android.util.Log.e("history", "no ..hr..data....");
}
}
});
} else {
result.notImplemented();
}
}
static Future<dynamic> syncHealthHr() async {
var healthDataList =
await _channel.invokeMethod('syncHealthData', {'dataType': 1208});
return healthDataList;
}
CodePudding user response:
It looks like onDataResponse()
is being invoked on a background thread, and you need to make sure result.success()
is called back on the main thread. To do this, you can use a Handler
.
Java 8:
new Handler(Looper.getMainLooper()).post(() -> { result.success(lists); });
Java 7:
new Handler(Looper.getMainLooper()).post(new Runnable() {
@Override
public void run() {
result.success(lists);
});