share method

Future<void> share(
  1. String url,
  2. String name, {
  3. String? checksum,
})

Downloads a file from the provided url and opens Share dialog with it.

Implementation

Future<void> share(String url, String name, {String? checksum}) async {
  // Provided file might already be cached.
  Uint8List? data;
  if (checksum != null && CacheWorker.instance.exists(checksum)) {
    data = (await CacheWorker.instance.get(checksum: checksum)).bytes;
  }

  if (data == null) {
    final Directory temp = await getTemporaryDirectory();
    final String path = '${temp.path}/$name';
    await (await dio).download(url, path);
    await Share.shareXFiles([XFile(path)]);
    File(path).delete();
  } else {
    await Share.shareXFiles([XFile.fromData(data, name: name)]);
  }
}