DownloadTask class final
A download of an object into a file that can be paused, resumed and canceled, and reports its progress. It
starts when it is created (by startDownloadToFile of package:firebase_storage_dart/io.dart); await
result for the metadata of the object.
The task reads the metadata of the object first (its size and generation), then writes the content to a
new file next to the target (<file>.<random>.part, created exclusively), and renames it over the target
once it holds exactly the size of the metadata. When the task fails or is canceled, the partial file is
deleted and the target is left untouched. The partial file is opened by its path right after the exclusive
create, so someone who can write to the directory could swap it in between; keep downloads in a directory
that only your program writes to.
Requests ask for Accept-Encoding: identity, so offsets count stored bytes. An object stored with
Content-Encoding: gzip arrives unzipped by the server, which ignores ranges for it: the task asks for the
whole object again after a pause or a failure and skips the bytes it has, and does not compare the length
with the stored size.
pause closes the connection, since a server times out a response that is held open. resume asks for
the rest with Range: bytes=<written>-. When the body breaks on the network (a closed connection, the idle
timeout of FirebaseStorage.requestTimeout), the task waits (1 s, doubling up to 64 s) and asks for the
rest the same way; it gives up with retry-limit-exceeded once the failures have lasted the download time
of the StorageRetryPolicy. A response that brought bytes resets both the wait and that time.
Each response is checked against the generation of the metadata (its x-goog-generation header), so the
file is never joined from two versions of the object: an object overwritten during the download fails the
task with object-changed. A server that does not send the header gets a metadata read before each
resumed request and once more before the rename. A server that answers a ranged request with the whole
object (status 200, as the emulator does for ranges it cannot satisfy) is read past the bytes already
written; a partial answer (206) that does not start at the offset asked for fails the task with unknown.
final DownloadTask task = storage.ref('videos/clip.mp4').startDownloadToFile(File('clip.mp4'));
task.progress.listen((DownloadProgress p) {
print('${p.state.name}: ${p.bytesTransferred} of ${p.totalBytes} bytes');
});
final FullMetadata metadata = await task.result;
Properties
- bytesTransferred → int
-
The bytes written to the partial file so far; they stay there across pauses and failures.
no setter
- file → File
-
The file the object goes to.
final
- hashCode → int
-
The hash code for this object.
no setterinherited
-
progress
→ Stream<
DownloadProgress> -
The progress of the download: the current snapshot when you listen, then events while bytes are written
(at most one per 64 KiB and 100 ms), when the metadata gives the size, and on each change of state
(DownloadState.paused, back to DownloadState.running, and the final one). The stream closes after the
final event; errors are not sent on it but in result. It can be listened to any number of times, and
earlier events are not replayed.
no setter
- ref → Reference
-
The reference of the object being downloaded.
final
-
result
→ Future<
FullMetadata> -
Completes with the metadata of the object once the file is in place, or fails: with a
FirebaseStorageException(canceledafter cancel,object-changedwhen the object was overwritten during the download), or with a FileSystemException when the file cannot be written.no setter - runtimeType → Type
-
A representation of the runtime type of the object.
no setterinherited
- snapshot → DownloadProgress
-
The current state as a DownloadProgress.
no setter
- state → DownloadState
-
The state of the task.
no setter
- totalBytes → int
-
The size of the object from its metadata; 0 until the metadata is read. When the server sends more than
that (an object stored with
Content-Encoding: gziparrives decompressed), it follows the bytes written.no setter
Methods
-
cancel(
) → bool -
Cancels a running or paused task: the partial file is deleted and result fails with
canceled. Returns false when the task already ended (or is being canceled). -
noSuchMethod(
Invocation invocation) → dynamic -
Invoked when a nonexistent method or property is accessed.
inherited
-
pause(
) → bool - Pauses a running task: the connection is closed, the bytes written so far stay. Returns false when the task is not running. After true, the paused event follows on progress unless the task ends first.
-
resume(
) → bool - Resumes a paused task from the bytes already written. Returns false when the task is not paused (or pausing).
-
toString(
) → String -
A string representation of this object.
inherited
Operators
-
operator ==(
Object other) → bool -
The equality operator.
inherited