Пожалуйста, помогите мне.
Мой плагин Flutter Java не может вернуть список из собственного Android, пожалуйста, скажите мне, как это решить.
Я создал плагин флаттера для подключения к своим часам. Я создал метод Chanel для выполнения собственного SDK для Android, но я застрял, потому что не могу отправить значение обратно в флаттер в этой функции, но другая часть работает нормально. Беспокоить всех вас, помогите мне решить эту проблему тоже.
эта ошибка
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)
Пример канала My Code Method
@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;
}
Похоже, что onDataResponse()
вызывается в фоновом потоке, и вам нужно убедиться, что result.success()
вызывается обратно в основном потоке. Для этого вы можете использовать Handler
.
Ява 8:
new Handler(Looper.getMainLooper()).post(() -> { result.success(lists); });
Ява 7:
new Handler(Looper.getMainLooper()).post(new Runnable() {
@Override
public void run() {
result.success(lists);
});
Большое спасибо, я смог решить проблему с помощью вашего предложения.