在我的Dart程序中,我正在使用相机:
...
final File imageProduct = await ImagePicker.pickImage(source: ImageSource.camera, maxHeight: 1024, maxWidth: 1024);
...
在以前的版本中,它运行良好,现在,在Flutter升级(v1.7.8 + hotfix.3)之后,显示了相机界面,我可以拍摄照片,之后出现错误:
W/Binder ( 7331): Caught a RuntimeException from the binder stub implementation.
W/Binder ( 7331): java.lang.RuntimeException: Methods marked with @UiThread must be executed on the main thread. Current thread: Binder:7331_4
在Flutter中,异步函数在主线程中执行,所以我真的不明白可能是什么问题。 我找不到在主线程中强制执行的明确方法。
可能是解决方案。
谢谢。
如果我更改代码:
File imageProduct;
try {
imageProduct = await ImagePicker.pickImage(
source: ImageSource.camera, maxHeight: 1024, maxWidth: 1024);
}
catch (err) {
print('>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> $err');
}
不显示摄像头界面,消息变为:
E/MethodChannel#plugins.flutter.io/image_picker( 8509): Failed to handle method call
E/MethodChannel#plugins.flutter.io/image_picker( 8509): java.lang.IllegalStateException: Reply already submitted
E/MethodChannel#plugins.flutter.io/image_picker( 8509): at io.flutter.embedding.engine.dart.DartMessenger$Reply.reply(DartMessenger.java:135)
更多代码(代码是从一个大模块中提取并简化的):
class _BarcodeHomePageState extends State<BarcodeHomePage> {
String _filename;
String _stareprogram = 'No image captured';
/* ---------------------------------------------------------------------------
* capture imagine + resize it
--------------------------------------------------------------------------- */
Future takeImage(String smode) async {
await getImage(smode);
String fname = '$_myExternalDirectory/XYZ/' + _filename + '.jpg';
if (_filename != null) {
if (FileSystemEntity.typeSync(fname) != FileSystemEntityType.notFound) {
await compute(resizeImage, fname).then((_){refreshlist(fname);});
}
}
}
Future<void> getImage(String smode) async {
String _filename = '001';
/* capture picture */
final File imageProduct = await ImagePicker.pickImage(source: ImageSource.camera, maxHeight: 1024, maxWidth: 1024);
if (imageProduct != null) {
String extJpg = '$_myExternalDirectory/XYZ/' + _filename + '.jpg';
await imageProduct.copy(extJpg)
.then((_) { print('>>>>>>>>> Image copied ' + extJpg + ' ...'); })
.catchError((e) { print('>>>>>>>> Image couldn''t be copied: ' + e.toString()); });
File imf = new File(extJpg);
Image newimg = Image.memory(Uint8List.fromList(imf.readAsBytesSync()));
_listContent.add(new bcImage(_nrord.toString().padLeft(2, '0'),
extJpg,
newimg, true));
_showSnackBar('New image added');
/* Refresh stare */
setState(() {
});
}
else {
setState(() {
if (_listContent.isEmpty) {
_stareprogram = 'No image captured';
}
});
}
}
...
}
class bcImage {
String nrord;
String filename;
Image image;
bool waiting;
bcImage(this.nrord, this.filename, this.image, this.waiting);
}