withGUI method

Future<bool> withGUI({
  1. String fileExtension = 'fmtc',
  2. bool? forceFilePicker,
  3. BuildContext? context,
})

Export the store with a graphical user interface (uses manual internally)

Set forceFilePicker to:

  • true: always force an attempt at using the file picker. This will cause an error on unsupported platforms, and so is not recommended.
  • false: always force an attempt at using the share dialog/sheet. This will cause an error on unsupported platforms, and so is not recommended.
  • null: uses the platform specifc file picker on Windows, MacOS, or Windows, and the share dialog/sheet on other platforms (inferred to be Android or iOS).

context (BuildContext) must be specified if using the share dialog/sheet, so it is necessary to pass it unless forceFilePicker is true. Will cause an unhandled null error if not passed when necessary.

Exported files are named as the store name plus the fileExtension ('fmtc' by default).

Returns true when successful, otherwise false when unsuccessful or unknown.

Implementation

Future<bool> withGUI({
  String fileExtension = 'fmtc',
  bool? forceFilePicker,
  BuildContext? context,
}) async {
  if (forceFilePicker ??
      Platform.isLinux || Platform.isMacOS || Platform.isWindows) {
    final String? outputPath = await FilePicker.platform.saveFile(
      dialogTitle: 'Export Cache Store',
      fileName: '${_storeDirectory.storeName}.$fileExtension',
      type: FileType.custom,
      allowedExtensions: [fileExtension],
    );

    if (outputPath == null) return false;

    await manual(File(outputPath));
    return true;
  } else {
    final File exportFile =
        _access >>> '${_storeDirectory.storeName}.$fileExtension';
    final box = context!.findRenderObject() as RenderBox?;

    await manual(exportFile);
    final ShareResult result = await Share.shareXFiles(
      [XFile(exportFile.absolute.path)],
      sharePositionOrigin: box!.localToGlobal(Offset.zero) & box.size,
    );
    await exportFile.delete();

    return result.status == ShareResultStatus.success;
  }
}