iOS app crashes when calling this function 2 times in a row (Firebase Storage, Flutter)

◇◆丶佛笑我妖孽 提交于 2021-02-11 12:50:56

问题


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.

From this button :

 floatingActionButton: FloatingActionButton(
        onPressed: () => _submit(),

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

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 :

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

回答1:


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



来源:https://stackoverflow.com/questions/66048537/ios-app-crashes-when-calling-this-function-2-times-in-a-row-firebase-storage-f

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!