Изображение, прикрепленное к плагину Flutter Share.shareFiles, иногда исчезает. Как это исправить?

#image #flutter #share

Вопрос:

Это мой код для обмена изображением с URL-адреса. Поэтому он сначала загружает изображение, а затем прикрепляет изображение с помощью общего листа.

   static Future<Null> onShareUrl({BuildContext context, String url, String title, String caption}) async {
    final RenderBox box = context.findRenderObject();
    if (Platform.isAndroid) {
      var response = await Network.getImage(url);
      final documentDirectory = (await getExternalStorageDirectory()).path;
      File imgFile = new File('$documentDirectory/attachment.png');
      imgFile.deleteSync();
      imgFile.writeAsBytesSync(response.bodyBytes);

      await Share.shareFiles(["$documentDirectory/attachment.png"], mimeTypes: ["image/png"], subject: title, text: caption, sharePositionOrigin: box.localToGlobal(Offset.zero) amp; box.size);
    }
  }
 

Я называю эту функцию так:

 onShareUrl(context: context, url: imageUrl, title: "This is the image", caption: "Please check this image");
 

Один раз это привело к появлению общего листа с правильным вложением:

Общий лист с правильным вложением

But other time, it resulted as share sheet without the attachment:

enter image description here

Well, to be precise, when the share sheet is just opened and still loading all the apps that may process the attachment, it shows the attachment. But a few seconds later, when the apps below loaded fully, the attachment is gone. That means the attachment is downloaded and attached successfully, but there’s something happens when the share sheet is loading, and the attachment is then gone a few seconds later.

It seems to happen at random. The disappearing does not happen consistently.

This happens just by tapping the same action button, within the same session. Nothing on the code changes. And I can confirm that the image is always retrieved successfully from the server.

Why is this happening? How can I prevent the attachment from disappearing? At first, I thought it was because of some issue when the IO tries to overwrite the existing file, so I added the deleteSync call. But the issue still persists apparently.