我在使用插件 image_picker: "0.6.1+11"和
条形码扫描:“^1.0.0”。首先我扫描条形码,然后从相机拍照。
问题是大多数情况下,android 应用程序在没有任何日志的情况下从相机拍摄图像后会自行重启。日志只打印“丢失与设备的连接”。
这是代码:
Future scanCarton() async {
try {
String barcode = await BarcodeScanner.scan();
await getImage().then((imageFile){
if (imageFile != null){
Map<String,String>requestData = new Map<String,String>();
requestData["content"] = barcode;
requestData["type"] = "carton";
requestData["mode"] = "job";
if (widget.orderData.product_type.toLowerCase() == "batched"){
requestData["carton_id"] = "0";
}else{
requestData["carton_id"] = "${scannedCartonCount + 1}";
}
if(this.mounted){
setState(() {
_isLoading = true;
});
}else{
print("Gotcha 2");
}
upload(imageFile, UrlFile.UPLOAD_SCAN_IMAGE, "carton", requestData).then((value){
if(this.mounted){
setState(() {
_isLoading = false;
});
}else{
print("Gotcha 4");
}
incrementCartonCount();
if(widget.orderData.product_type.toLowerCase() == "batched"){
openNextCartonPopup();
}
});
}
});
} on PlatformException catch (e) {
if (e.code == BarcodeScanner.CameraAccessDenied) {
print('The user did not grant the camera permission!');
} else {
print('Unknown error $e') ;
}
} on FormatException{
print( 'null (User returned using the "back"-button before scanning anything. Result)');
} catch (e) {
print('Unknown error: $e');
}
}
最佳答案
您只需要事先配置以请求许可。
对于 iOS,将以下键添加到您的 Info.plist
文件,位于 <project root>/ios/Runner/Info.plist
:
Privacy - Photo Library Usage Description
在可视化编辑器中。 Privacy - Camera Usage Description
在可视化编辑器中。 Privacy - Microphone Usage Description
在可视化编辑器中。 同样的,你需要在Android中添加权限。
关于flutter - 在 Flutter 中失去与设备的连接?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58830884/