DEV Community

mukhtharcm
mukhtharcm

Posted on • Originally published at mukhtharcm.com on

Sharing Files in Flutter

Sharing is an integral part of mobile apps. Millions of digital content are produced and consumed every minute. Because of this, we can’t avoid a feature like this on our mobile apps.

There is a package named share in Flutter to do just this. it can be used to share just text or even Files.

First, let’s add the package to our pubspec.yaml file.

dependencies:
flutter:
sdk: flutter
share: ^2.0.1 # add the latest version from pub.dev

Enter fullscreen mode Exit fullscreen mode

To share a simple text, use Share.share() anywhere on our App.

Share.share("Your Text to Share");

Enter fullscreen mode Exit fullscreen mode

It will bring up the share dialog of respective platform, whether if you’re using Android or iOs.

If we have to share Files, we can use Share.shareFiles()it accepts a list of File Paths to share.

File mycoolImage;
Share.share([mycoolImage.path,]);

Enter fullscreen mode Exit fullscreen mode

below is the same example from Generate Image from Widget Tutorial, but with the added function of sharing the generated Image.

Future<void> takePicture() async {
RenderRepaintBoundary boundary = genKey.currentContext.findRenderObject();
ui.Image image = await boundary.toImage();
final directory = (await getApplicationDocumentsDirectory()).path;
ByteData byteData = await image.toByteData(format: ui.ImageByteFormat.png);
Uint8List pngBytes = byteData.buffer.asUint8List();
File imgFile = new File('$directory/photo.png');
await imgFile.writeAsBytes(pngBytes);
await Share.shareFiles([imgFile.path]);//Sharing the generated Image
}

Enter fullscreen mode Exit fullscreen mode

Hope you found something useful here.

I’m always open to suggestions!

Let me know your suggestions and opinions in Twitter DM or drop a mail a mukhtharcm@gmail.com.

If you’re feeling too generous, you can support me through Buy Me a Coffee.

Finally, if you found this helpful, please share this within your reach so that more people can benefit from this. And Follow me on Twitter for getting more posts like these 😉.

Qodo Takeover

Introducing Qodo Gen 1.0: Transform Your Workflow with Agentic AI

While many AI coding tools operate as simple command-response systems, Qodo Gen 1.0 represents the next generation: autonomous, multi-step problem-solving agents that work alongside you.

Read full post

Top comments (0)

Qodo Takeover

Introducing Qodo Gen 1.0: Transform Your Workflow with Agentic AI

Rather than just generating snippets, our agents understand your entire project context, can make decisions, use tools, and carry out tasks autonomously.

Read full post