Flutter Method Chanel Error java.lang.RuntimeException: 标有@UiThread 的方法必须在主线程上执行

Flutter Method Chanel Error java.lang.RuntimeException: Methods marked with @UiThread must be executed on the main thread

请帮助我。

我的 Flutter Plugin Java 无法 return 原生列表 android 请告诉我解决它。

我创建了一个 flutter 插件来连接我的手表。我创建了一个 Chanel 方法来执行 android 本机 SDK,但我被卡住了,因为我无法在此函数中将值发送回 flutter,但其他部分工作正常。打扰大家也帮我解决这个问题

这个错误

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.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.run(BluetoothGatt.java:478)
W/System.err(21058):    at android.bluetooth.BluetoothGatt.runOrQueueCallback(BluetoothGatt.java:780)
W/System.err(21058):    at android.bluetooth.BluetoothGatt.access0(BluetoothGatt.java:41)
W/System.err(21058):    at android.bluetooth.BluetoothGatt.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)

示例我的代码方法频道

@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.

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);
  });