withGUI method

Future<Map<String, Future<bool>>?> withGUI({
  1. String fileExtension = 'fmtc',
  2. bool emptyCacheBeforePicking = true,
})

Import store(s) with a graphical user interface (uses manual internally)

Uses the platform specifc file picker. Where supported, limits file extension to fileExtension ('fmtc' by default), otherwise any file can be selected as a fallback.

It is recommended to leave emptyCacheBeforePicking as the default (true). Otherwise, the picker may use cached files as opposed to the real files, which may yield unexpected results. This is only effective on Android and iOS - other platforms cannot use caching.

If any files are selected, a Map is returned: where the keys are the the selected filenames (without extensions), and the values will resolve to a bool specifying whether the import was successful or unsuccessful. Otherwise null will be returned.

Implementation

Future<Map<String, Future<bool>>?> withGUI({
  String fileExtension = 'fmtc',
  bool emptyCacheBeforePicking = true,
}) async {
  if (emptyCacheBeforePicking && (Platform.isAndroid || Platform.isIOS)) {
    await FilePicker.platform.clearTemporaryFiles();
  }

  late final FilePickerResult? importPaths;
  try {
    importPaths = await FilePicker.platform.pickFiles(
      dialogTitle: 'Import Cache Stores',
      type: FileType.custom,
      allowedExtensions: [fileExtension],
      allowMultiple: true,
    );
  } on PlatformException catch (_) {
    importPaths = await FilePicker.platform.pickFiles(
      dialogTitle: 'Import Cache Stores',
      allowMultiple: true,
    );
  }

  if (importPaths == null) return null;

  return Map.fromEntries(
    importPaths.files.where((f) => f.extension == fileExtension).map(
          (pf) => MapEntry(
            p.basenameWithoutExtension(pf.name),
            manual(File(pf.path!)),
          ),
        ),
  );
}