fetchSavedBridges static method

Future<List<Bridge>> fetchSavedBridges([
  1. Directory? directory
])

Fetch all of the bridges already saved to the user's device.

If the bridges are not saved to the default folder location, provide their location with directory.

Implementation

static Future<List<Bridge>> fetchSavedBridges([Directory? directory]) async {
  List<Bridge> bridges = [];

  if (kIsWeb) {
    // cookies instead of local storage (not yet implemented)
  } else {
    final Directory dir = directory ??
        Directory(
          await MyFileExplorerSDK.createPath(
            localDir: LocalDir.appSupportDir,
            subPath: Folders.bridgesSubPath,
          ),
        );

    /// Create the directory if it does not exist.
    if (!dir.existsSync()) {
      dir.createSync(recursive: true);
      return [];
    }

    // Get the bridge files from the directory.
    final List<FileSystemEntity> entities = await dir.list().toList();
    final List<File> bridgeFiles = entities
        .whereType<File>()
        .where((file) =>
            MyFileExplorerSDK.tryParseFileExt(file.path)?.toLowerCase() ==
            ".json")
        .toList();

    /// Parse the bridge files into [Bridge] objects.
    for (File bridgeFile in bridgeFiles) {
      String fileContents = bridgeFile.readAsStringSync();

      Map<String, dynamic> bridgeJson = JsonTool.readJson(fileContents);

      bridges.add(Bridge.fromJson(bridgeJson));
    }
  }

  return bridges;
}