UploadTask class final
An upload that can be paused, resumed and canceled, and reports its progress. It starts when it is created
(by Reference.startUpload or Reference.startStreamUpload); await result for the metadata of the new
object.
Up to 256 KiB the data goes in one multipart request, like Reference.putBytes. Larger data uses a
resumable upload session:
- On the VM, bytes (
Reference.startUpload) and files (putFileofpackage:firebase_storage_dart/io.dart) go in one streamed request with the rest of the data. After a failure or a pause the task asks the server how much it has and streams the rest from there. - A stream (
Reference.startStreamUpload), everything on the web, and every upload of aFirebaseStoragewith amaxUploadChunkSizego in chunks: they start at 256 KiB and double after each one that succeeds, up to 16 MiB (ormaxUploadChunkSize). A stream needs chunks because the chunk is what is sent again after a failure; the web, because the browser reports no progress inside a request. - A stream without a
lengthalways uses a session, whatever its size. The session starts without the total, totalBytes is -1 until the stream ended, and each chunk is sent once the task has read one stream event past it (or the end), so that the last chunk carriesupload, finalize.
When a request fails with a network error or a retryable status (5xx, 408, 429), the task waits (1 s,
doubling up to 64 s), asks the server how much it has, and goes on from there (with a 256 KiB chunk when
chunked); it gives up with retry-limit-exceeded once the failures have lasted the upload time of the
StorageRetryPolicy. A success resets both the wait and that time.
At most FirebaseStorage.maxConcurrentUploads uploads of one instance run at once. A task over the limit
waits its turn in UploadState.running with 0 bytes; pause and cancel work while it waits, and a
paused task gives its turn to the next one (it queues again when resumed).
final UploadTask task = storage.ref('videos/clip.mp4').startUpload(bytes);
task.progress.listen((UploadProgress p) {
print('${p.state.name}: ${p.bytesTransferred} of ${p.totalBytes} bytes');
});
final FullMetadata metadata = await task.result;
On the VM, progress also reports the bytes as they are written while a request is being sent, at most once per 64 KiB and per 100 ms, and always when the server confirms a chunk. When a chunk fails, bytesTransferred goes back to what the server confirmed, with an event. On the web the browser gives no upload progress, so the events come once per confirmed chunk.
The task is not a Future; pausing aborts the request in flight and resuming asks the server where it
stopped. cancel works while running and while paused.
Properties
- bytesTransferred → int
-
The bytes sent so far: those the server has confirmed, plus (on the VM) those of the chunk being sent. It
goes back to the confirmed bytes when a chunk fails.
no setter
- hashCode → int
-
The hash code for this object.
no setterinherited
-
progress
→ Stream<
UploadProgress> -
The progress of the upload: the current snapshot when you listen, then events while data is sent (on
the VM at most one per 64 KiB and 100 ms, plus one per confirmed chunk; on the web one per chunk) and on
each change of state (UploadState.paused, back to UploadState.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 the data goes to.
final
-
result
→ Future<
FullMetadata> -
Completes with the metadata of the new object, or fails with a
FirebaseStorageException(canceledafter cancel). An error of the data stream ofReference.startStreamUploadis passed on as it is.no setter - runtimeType → Type
-
A representation of the runtime type of the object.
no setterinherited
- sessionUri → Uri?
-
The URI of the resumable upload session, once the server has created it; null for a one-shot upload (up
to 256 KiB) and before the session exists.
no setter
- snapshot → UploadProgress
-
The current state as an UploadProgress.
no setter
- state → UploadState
-
The state of the task.
no setter
- totalBytes → int
-
The size of the upload in bytes; -1 while it is unknown (a stream given to
Reference.startStreamUploadwithout alength, until the task has read its end). It changes to the size without an event on progress; the next event carries it.no setter
Methods
-
cancel(
) → bool -
Cancels a running or paused task: 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 request in flight is aborted. Returns false when the task is not running,
for example when it already finished, as
UploadTask.pauseof the JS SDK does. After true, the paused event follows on progress unless the task ends first (see progress). -
resume(
) → bool - Resumes a paused task; it first asks the server how much data it has. 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