Search code examples
flutterflutter-packagesflutter-image

Weird File type when trying to share widget screenshot in flutter


I am trying to use the Flutter Screenshot package with the Share Plus package to share images of widgets.

I finally thought I got it all working, but when I tested it I got this:

enter image description here

That doesn't look like an image filetype to me. Here is what I am doing with both packages:

ElevatedButton.icon(
            onPressed: () async {
              final imageFile = await screenshotController.capture();
              XFile file = XFile.fromData(imageFile!);
              Share.shareXFiles([file]);
            },
            icon: const Icon(Icons.share),
            label: const Text("Share Image"),
          ),

Solution

  • IMHO, the screenshotController.capture() will return an Uint8List image type, that why XFile treats it as a binary kind as expected. I suggest that you save the image first then upload it.

    _screenshotController.capture().then((Uint8List image) async {
      if (image != null) {
        final directory = await getApplicationDocumentsDirectory();
        final imagePath = await File('${directory.path}/image.png').create();
        await imagePath.writeAsBytes(image);
    
        /// Share Plugin
        await Share.shareFiles([XFile(imagePath)]);
      }
    });