getFileFromCID method

Future<File?> getFileFromCID(
  1. String cid
)

Given a compliant CID string, it fetches and caches that assets. NOTE: Because of limitations, this will NOT WORK on web

Implementation

Future<File?> getFileFromCID(String cid) async {
  // check for local existance of the file
  if (!kIsWeb) {
    try {
      // only inits if cache dir is empty
      (cacheDir == null || !cacheDir!.existsSync()) ? await init() : null;
      File cidCache = await getCacheFile(cid);
      if (cidCache.existsSync()) {
        return cidCache;
      } else {
        final Uint8List cidContents =
            await s5.api.downloadRawFile(CID.decode(cid).hash);
        if (cidContents.isNotEmpty) {
          await cidCache.writeAsBytes(cidContents);
          return cidCache;
        }
      }
    } catch (e) {
      print(e);
    }
  } else {
    // Files are NOT implemented on web.
    throw UnimplementedError();
  }
  return null;
}