download method

Future<FileContent> download(
  1. String path
)

Implementation

Future<FileContent> download(String path) async {
  String? name;
  String? mimeType;
  int? expectedSize;
  var bytesReceived = 0;
  final bytes = BytesBuilder(copy: false);
  final stream = await downloadStream(path);
  await for (final chunk in stream) {
    final kind = chunk.headers["kind"];
    if (kind == "start") {
      final chunkName = chunk.headers["name"];
      final chunkMimeType = chunk.headers["mime_type"];
      final chunkSizeValue = chunk.headers["size"];
      if (chunkName is! String || chunkMimeType is! String || chunkSizeValue is! int || chunkSizeValue < 0) {
        throw _unexpectedResponseError("download");
      }
      name = chunkName;
      mimeType = chunkMimeType;
      expectedSize = chunkSizeValue;
      continue;
    }
    if (kind != "data") {
      throw _unexpectedResponseError("download");
    }
    bytes.add(chunk.data);
    bytesReceived += chunk.data.length;
  }
  if (name == null || mimeType == null || expectedSize == null || bytesReceived != expectedSize) {
    throw _unexpectedResponseError("download");
  }
  return FileContent(data: bytes.takeBytes(), name: name, mimeType: mimeType);
}