程序员社区

【错误记录】Flutter 插件报错 ( Methods marked with @UiThread must be executed on the main thread. | 更新最新 SDK )

文章目录

  • 一、报错信息
  • 二、问题分析
  • 三、解决方案

一、报错信息


在 Flutter 中使用 image_picker 插件时 , 如 【Flutter】Flutter 拍照示例 ( 拍照源码示例 ) 中选择照片后 , 报如下错误 :

W/Binder  (30392): Caught a RuntimeException from the binder stub implementation.
W/Binder  (30392): java.lang.RuntimeException: Methods marked with @UiThread must be executed on the main thread. Current thread: Binder:30392_2
W/Binder  (30392): 	at io.flutter.embedding.engine.FlutterJNI.ensureRunningOnMainThread(FlutterJNI.java:992)
W/Binder  (30392): 	at io.flutter.embedding.engine.FlutterJNI.invokePlatformMessageResponseCallback(FlutterJNI.java:779)
W/Binder  (30392): 	at io.flutter.embedding.engine.dart.DartMessenger$Reply.reply(DartMessenger.java:144)
W/Binder  (30392): 	at io.flutter.plugin.common.MethodChannel$IncomingMethodCallHandler$1.success(MethodChannel.java:238)
W/Binder  (30392): 	at io.flutter.plugins.imagepicker.ImagePickerDelegate.finishWithSuccess(ImagePickerDelegate.java:540)
W/Binder  (30392): 	at io.flutter.plugins.imagepicker.ImagePickerDelegate.handleImageResult(ImagePickerDelegate.java:505)
W/Binder  (30392): 	at io.flutter.plugins.imagepicker.ImagePickerDelegate.access$000(ImagePickerDelegate.java:66)
W/Binder  (30392): 	at io.flutter.plugins.imagepicker.ImagePickerDelegate$4.onPathReady(ImagePickerDelegate.java:470)
W/Binder  (30392): 	at io.flutter.plugins.imagepicker.ImagePickerDelegate$3$1.onScanCompleted(ImagePickerDelegate.java:160)
W/Binder  (30392): 	at android.media.MediaScannerConnection$ClientProxy.onScanCompleted(MediaScannerConnection.java:204)
W/Binder  (30392): 	at android.media.MediaScannerConnection$1.scanCompleted(MediaScannerConnection.java:53)
W/Binder  (30392): 	at android.media.IMediaScannerListener$Stub.onTransact(IMediaScannerListener.java:61)
W/Binder  (30392): 	at android.os.Binder.execTransact(Binder.java:731)

在这里插入图片描述

二、问题分析


在 image_picker 的主页 , 已经对上述问题作出了说明 , 使用 image_picker 插件选择数据时 , 有时出现图像丢失的情况 , 此时使用 retrieveLostData 获取丢失的图像 ;

代码示例 :

Future<void> retrieveLostData() async {
  final LostData response =
      await picker.getLostData();
  if (response.isEmpty) {
    return;
  }
  if (response.file != null) {
    setState(() {
      if (response.type == RetrieveType.video) {
        _handleVideo(response.file);
      } else {
        _handleImage(response.file);
      }
    });
  } else {
    _handleError(response.exception);
  }
}

还有一个问题就是可能使用了过时的 Flutter 插件 ;

三、解决方案


更新 Flutter SDK 到最新版本 ;

到 https://flutter.dev/docs/development/tools/sdk/releases 页面下载最新的 Flutter SDK ;

在这里插入图片描述

下载完毕后 , 解压 , 放到一个目录中即可 ;

菜单栏 / File / Settings 对话框中设置最新的额 Flutter SDK 路径 ;

在这里插入图片描述

配置最新的 image_picker 插件 ;

dependencies:
  image_picker: ^0.7.2+1

在这里插入图片描述

使用最新版本的 image_picker 插件后 , 使用相应的图片获取方法 , 上述问题解决 ;

  Future getImage() async {
    final pickedFile = await picker.getImage(source: ImageSource.camera);

    setState(() {
      if (pickedFile != null) {
        _image = File(pickedFile.path);
      } else {
        print('No image selected.');
      }
    });
  }

赞(0) 打赏
未经允许不得转载:IDEA激活码 » 【错误记录】Flutter 插件报错 ( Methods marked with @UiThread must be executed on the main thread. | 更新最新 SDK )

相关推荐

  • 暂无文章

一个分享Java & Python知识的社区