plus_plugins icon indicating copy to clipboard operation
plus_plugins copied to clipboard

[Bug]: prior share-sheet did not call back, did you await it? Maybe use non-result variant

Open IlyaMax opened this issue 2 years ago • 24 comments

Platform

android 10

Plugin

share_plus

Version

6.2.0

Flutter SDK

3.0.4

Steps to reproduce

call this

const logFileName = 'log.txt';
Future<String> get logFilePath async =>
    '${await getApplicationDocumentsDirectory().then((value) => value.path)}/$logFileName';
Share.shareXFiles([XFile(await logFilePath)])

Code Sample

No response

Logs

E/flutter (13841): [ERROR:flutter/lib/ui/ui_dart_state.cc(198)] Unhandled Exception: PlatformException(Share callback error, prior share-sheet did not call back, did you await it? Maybe use non-result variant, null, null)
E/flutter (13841): #0      StandardMethodCodec.decodeEnvelope
package:flutter/…/services/message_codecs.dart:607
E/flutter (13841): #1      MethodChannel._invokeMethod
package:flutter/…/services/platform_channel.dart:167
E/flutter (13841): <asynchronous suspension>
E/flutter (13841): #2      MethodChannelShare.shareFilesWithResult
package:share_plus_platform_interface/method_channel/method_channel_share.dart:134
E/flutter (13841): <asynchronous suspension>
E/flutter (13841):

Flutter Doctor

[✓] Flutter (Channel stable, 3.0.4, on macOS 12.5.1 21G83 darwin-arm, locale en-UA)
    • Flutter version 3.0.4 at /Users/illia/Development/flutter
    • Upstream repository https://github.com/flutter/flutter.git
    • Framework revision 85684f9300 (5 months ago), 2022-06-30 13:22:47 -0700
    • Engine revision 6ba2af10bb
    • Dart version 2.17.5
    • DevTools version 2.12.2

Checklist before submitting a bug

  • [X] I Google'd a solution and I couldn't find it
  • [X] I searched on StackOverflow for a solution and I couldn't find it
  • [X] I read the README.md file of the plugin
  • [X] I'm using the latest version of the plugin
  • [X] All dependencies are up to date with flutter pub upgrade
  • [X] I did a flutter clean
  • [X] I tried running the example project

IlyaMax avatar Nov 28 '22 11:11 IlyaMax

I was facing the same issue on Android 12.

Code:

  Future<void> exportData() async {
    final String jsonString = jsonEncode(toJson());

    // Create a temporary file
    final directory = await pathProvider.getTemporaryDirectory();
    final File jsonFile = File(
        '${directory.path}/portarius_backup_${_parseTime(DateTime.now())}.json');
    await jsonFile.writeAsBytes(jsonString.codeUnits);

    final XFile file = XFile(jsonFile.path);
    ShareResult result = await Share.shareXFiles(
      [file],
      text: 'Portarius backup',
      subject: 'Portarius backup',
    );

    if (result.status != ShareResultStatus.success) {
      _logger.e('Error sharing file: ${result.status}');
    }

    // Delete the temporary file
    await jsonFile.delete();
  }

I fixed it by rebuilding the app (and cold booting the VM)—no clue what caused it or if it'll happen again.

zbejas avatar Dec 08 '22 13:12 zbejas

I'm having the same issue, did you find any solution?

RicardoRB avatar Dec 19 '22 16:12 RicardoRB

Having the same issue at my end, the share bottom sheet doesn't open and falls back to the exception as mentioned above by @IlyaMax

Use case: I have used shareXFiles() method to share multiple images and text on WhatsApp and tried making it a async-await call as mentioned in the logs.

Flutter SDK: 3.3.10 Android 8.0.0 (API 26)

Please resolve it as soon as possible.

RB-93 avatar Dec 27 '22 10:12 RB-93

Same problem, Ios fine, only Android

what2003 avatar Jan 05 '23 21:01 what2003

Same here! Let me know if you have a fix. Cheers

alexandrespriet-st avatar Jan 25 '23 11:01 alexandrespriet-st

I have faced the same problem, but mine was sharing after moving a file from the private folder to the Download folder. The solution for me was to first turn the path in a File and then use this file's path to make a new XFile.

After downloading, I moved the file to the Download Folder and was trying to create a new XFile from the moved file's path, which did not work. I then created a new File with that same path and used it's path and it worked. Maybe there is something hidden in the File creation process, but I hope this can help!

Future<String> downloadMethod() {
     final Response response = await _dio.get(
      url,
      options: Options(
        responseType: ResponseType.bytes,
        followRedirects: false,
      ),
    );

    final file = await _createFile(fileName);
    final uniqueFileName = _getFileName(file.path);
    _writeFile(file, response);

    await moveDownloadedFilesToDownloadDirectory([file.path]);

    final movedFilePath =
        _mediaStoreWrapper.getCompleteFilePath(fileName: uniqueFileName);
    final movedFile = File(movedFilePath);

    return movedFile.path;
}

My share implementation was then:

final paths = filePathList
        .map(
          (filePath) => XFile(filePath),
        )
        .toList(growable: false);
    final result = await Share.shareXFiles(
      paths,
      subject: subjectText,
      text: messageText,
    );

It worked like this :)

gabrielaraujoz avatar Feb 08 '23 15:02 gabrielaraujoz

Facing the same issue, any follow-ups?

  • android 12
  • share plus 6,3,1

JoaoVitorAmorim avatar Mar 02 '23 09:03 JoaoVitorAmorim

in my case am getting the following error: PlatformException (PlatformException(Share callback error, prior share-sheet did not call back, did you await it when using below code:

final XFile file = XFile(path);
await Share.shareXFiles([file],

but if I use the deprecated method like below (passing the List<String>, the sharing of image works perfectly: await Share.shareFiles([path], subject: subject, text: text, sharePositionOrigin: box.localToGlobal(Offset.zero) & box.size);

shareFiles is marked as deprecated but works fine, while following documentation and using shareXFiles, throws the plaform exception error: My environment: Flutter 3.7.10 • channel stable • https://github.com/flutter/flutter.git Framework • revision 4b12645012 (9 days ago) • 2023-04-03 17:46:48 -0700 Engine • revision ec975089ac Tools • Dart 2.19.6 • DevTools 2.20.1

IncognitoGK9 avatar Apr 13 '23 08:04 IncognitoGK9

Based on my understanding, it appears that the occurrence is triggered by multiple calls. Therefore, I have taken the initiative to include a loader in order to prevent the issue from arising due to such repeated calls.

harshparikhfb avatar Apr 18 '23 12:04 harshparikhfb

Based on my understanding, it appears that the occurrence is triggered by multiple calls. Therefore, I have taken the initiative to include a loader in order to prevent the issue from arising due to such repeated calls.

Could you elaborate more?

rafaelpadu avatar Apr 22 '23 21:04 rafaelpadu

-> Show loader -> Share functionality -> Dismiss loader

harshparikhfb avatar Apr 25 '23 06:04 harshparikhfb

-> Show loader -> Share functionality -> Dismiss loader

give an example

IncognitoGK9 avatar Apr 25 '23 06:04 IncognitoGK9

I am currently facing an issue where I am unable to share images on iOS devices. I need to fix this problem. Please let me know if it would be fine.

harshparikhfb avatar Apr 25 '23 07:04 harshparikhfb

any solution?

Falchizao avatar May 22 '23 20:05 Falchizao

In android 12/13 im getting this same issue but in previous versions its just working fine..

imsamgarg avatar May 24 '23 18:05 imsamgarg

I fixed the error by ensuring that I used the "await" keyword when calling my use case, which contains all the shared logic. Initially, I neglected to include "await" in the first call.

iCarvalho7 avatar May 28 '23 00:05 iCarvalho7

yeah,use await at first

superAndroidMan avatar Jun 06 '23 09:06 superAndroidMan

I was able to resolve this issue by downgrading share_plus version from 7.0.2 to 7.0.0.

Code:

`final directoryPath = (await getTemporaryDirectory()).path;
  final imagePath = '$directoryPath/qrimg.png';
  final imageFile = await File(imagePath).create(recursive: true);
  imageFile.writeAsBytesSync(byteData.buffer.asUint8List());

  final file = XFile(imagePath);
  await Share.shareXFiles([file]);`

Flutter SDK - 3.7.11 Android version 12

jinal-dev avatar Jun 16 '23 10:06 jinal-dev


                            final Directory temporaryDirectory = await getTemporaryDirectory();
/// getTemporaryDirectory() is from path_provider
                            final String path = "${temporaryDirectory.path}/image.jpg";
                            File(path).writeAsBytesSync(qrImage!);
  /// Type of qrImage is Uint8List
                            await Share.shareXFiles([XFile(path)]);

this solution worked for me Hope this will work for you too.

virajtalaviya avatar Jun 18 '23 13:06 virajtalaviya


                            final Directory temporaryDirectory = await getTemporaryDirectory();
/// getTemporaryDirectory() is from path_provider
                            final String path = "${temporaryDirectory.path}/image.jpg";
                            File(path).writeAsBytesSync(qrImage!);
  /// Type of qrImage is Uint8List
                            await Share.shareXFiles([XFile(path)]);

this solution worked for me Hope this will work for you too.

This worked for me! Thanks!

rafaelpadu avatar Jun 30 '23 12:06 rafaelpadu

I fixed it by rebuilding the app (and cold booting the VM)—no clue what caused it or if it'll happen again.

Cold reboot emu helped me too.

smka avatar Aug 05 '23 22:08 smka

This issue is stale because it has been open 60 days with no activity. Remove stale label or comment or this will be closed in 15 days

github-actions[bot] avatar Nov 04 '23 00:11 github-actions[bot]

Same error on real device. Android

erperejildo avatar Nov 18 '23 09:11 erperejildo

Fix by blocking multiple calls to share method.

Example you have image share button & user clicks it multiple time.

glintpursuit avatar Jan 11 '24 09:01 glintpursuit

Fix by blocking multiple calls to share method.

Example you have image share button & user clicks it multiple time.

@glintpursuit This solution working ok

hoanglm-guide-vn avatar Feb 28 '24 03:02 hoanglm-guide-vn

I'm already using await and there's no way anyone else would call this function simultaneously since it's a button click handler and I'm not rage clicking. I have a suspicion that there's a lock file or mutex which left open somehow from one of my previous interrupted debug sessions and the Android native stack thinks now permanently that it's in a call? Is that possible? Yesterday this was working fine for me.

MrCsabaToth avatar Apr 05 '24 22:04 MrCsabaToth

I have a suspicion that there's a lock file or mutex which left open somehow from one of my previous interrupted debug sessions and the Android native stack thinks now permanently that it's in a call? Is that possible?

Take a look at the open pr #2817 which throws some light on the problem with the ShareSuccessManager implementation getting deadlocked.

Essentially, a further call to the share files while the previous share file call hasn't returned will cause the second call to be ignored. The problem is that a failed first call would cause the plugin to be in a bad state. The changes in the linked PR try to address that. If we see that those changes are insufficient, we will have to rethink the implementation of ShareSuccessManager and/or remove the "result" from the share operation to allow multiple calls.

miquelbeltran avatar Apr 06 '24 06:04 miquelbeltran

Take a look at the open pr #2817 which throws some light on the problem with the ShareSuccessManager implementation getting deadlocked.

Essentially, a further call to the share files while the previous share file call hasn't returned will cause the second call to be ignored. The problem is that a failed first call would cause the plugin to be in a bad state. The changes in the linked PR try to address that. If we see that those changes are insufficient, we will have to rethink the implementation of ShareSuccessManager and/or remove the "result" from the share operation to allow multiple calls.

Essentially a lot of people here I'm sure don't call the share simultaneously, and probably have await as me, and possibly ran into that behavior you mention. The question is that as a workaround how can you make the system "snap out of it"?

MrCsabaToth avatar Apr 06 '24 06:04 MrCsabaToth

The question is that as a workaround how can you make the system "snap out of it"?

Not really a workaround, but rather correctly restarting the state of the ShareSuccessManager

As a "hack", you can provoke an error (e.g. attempting to share a non-existing file) and that resets the state when an error occurs.

If after that we still get reports regarding this issue, then I will reimplement the ShareSuccessManager from scratch, or maybe even remove the "result" functionality from the share methods, because I think having a stable package is more important than having that functionality.

miquelbeltran avatar Apr 06 '24 08:04 miquelbeltran

I was able to snap out of it by making code changes. I didn't do any flutter clean.

MrCsabaToth avatar Apr 07 '24 17:04 MrCsabaToth