问题描述
我的应用程序在调用"_submit"时崩溃连续运行两次.我可以从图库中挑选图片并将其上传到Firebase Storage,但是如果再次调用它,整个应用程序将崩溃.
My app crashes when calling "_submit" function 2 times in a row.I can pick the picture from gallery and upload it to Firebase Storage but if I call it again the the whole app crashes.
通过此按钮:
floatingActionButton: FloatingActionButton(
onPressed: () => _submit(),
Submit调用类型为Database的提供者:
Submit calls a Provider of type Database :
Future<void> _submit() async {
widget.database = Provider.of<Database>(context, listen: false);
await widget.database
.setPicture("regione/citta/comune/lavoro/IDArtista/profilo.png");
return;
}
该函数调用上传从"imgGallery()"拍摄的图片的函数.到数据库:
That calls a function that uploads a picture taken from "imgGallery()" to the database :
Future<void> setPicture(String pathStorage) async {
try {
final File file = await imgFromGallery();
if (file == null) return;
TaskSnapshot task =
await FirebaseStorage.instance.ref(pathStorage).putFile(file);
String image_url = await task.ref.getDownloadURL();
return;
} catch (e) {
print(e);
return;
}
}
imgGallery:
imgGallery :
Future<File> imgFromGallery() async {
try {
final ImagePicker _picker = ImagePicker();
final PickedFile imageFile =
await _picker.getImage(source: ImageSource.gallery, imageQuality: 50);
//If there is no image selected, return.
if (imageFile == null) return null;
//File created.
File tmpFile = File(imageFile.path);
//it gives path to a directory - path_provider package.
final appDir = await getApplicationDocumentsDirectory();
//filename - returns last part after the separator - path package.
final fileName = tmpFile.path.split('/').last;
//copy the file to the specified directory and return File instance.
return tmpFile = await tmpFile.copy('${appDir.path}/$fileName');
} catch (e) {
print(e);
return null;
}
}
使用实际设备而不是仿真器解决.
EDIT : Solved using a real device instead of emulators.
推荐答案
您在哪个设备中遇到此问题?我也有此错误,但仅在iOS模拟器上.它与Image_Picker包和FocusNode有关.在github上查看此问题
Which device are you experiencing this in? I'm also having this error but only on iOS emulator. It has to do with the Image_Picker package and the FocusNode. Look at this issue on github
这篇关于连续调用此函数两次(Firebase存储,Flutter)时,iOS应用程序崩溃的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!