uploadFile method
UploadTask
uploadFile({})
You can upload file using File or Uint8List byte array.
If you upload file using File:
-
provide only
file
argument.
If you upload file using Uint8List:
-
specify
data
andchildPath
(which is used as the filename) arguments.
Implementation
UploadTask uploadFile({
File? file,
Uint8List? data,
String? childPath,
String? visibleName,
SettableMetadata? metadata,
String? refPath,
}) {
assert(file != null || data != null, 'file OR data must be provided');
assert(!(file != null && data != null), 'file OR data must be provided');
assert(data == null || childPath != null, 'then data is provided, specify the childPath');
var storageRef = FirebaseStorage.instance.ref(refPath);
if (childPath != null) {
storageRef = storageRef.child(childPath);
}
late UploadTask uploadTask;
if (file != null) {
uploadTask = storageRef.putFile(file, metadata);
} else if (data != null) {
uploadTask = storageRef.putData(data, metadata);
}
final task = FirefileTask.fromUploadTask(
uploadTask: uploadTask,
visibleName: visibleName,
onUpdate: (task) => _bloc.add(FirefileEvent.updateTask(task, eventId: ++eventId)),
);
final existingTaskIndex = tasks.indexWhere((t) => task.fullPath == t.fullPath);
if (existingTaskIndex > -1) {
tasks[existingTaskIndex] = task;
} else {
tasks.add(task);
}
_bloc.add(FirefileEvent.updateTaskList(tasks, eventId: ++eventId));
return uploadTask;
}