Disk class

One named place to keep files: a folder on the device, the app bundle, memory, or any DiskDriver.

Get one with disk('name') or FileStorage.disk('name'). Paths are relative to the disk and always use /; .. can never climb above the disk's root. Every failure throws a DiskException that names the disk and the path.

await disk('documents').put('reports/q3.pdf', pdfBytes);
final String avatar = await disk().putFile('avatars', photo);
final List<Order>? orders = await disk().json<List<Order>>('orders.json');
Implementers

Constructors

Disk(String name, DiskDriver driver, {String prefix = '', bool readOnly = false, bool protectRoot = false, Duration? prune})
A disk named name over driver, rooted at prefix inside it.

Properties

driver → DiskDriver
The backend that stores the bytes.
final
hashCode → int
The hash code for this object.
no setterinherited
name → String
The disk's name, e.g. local.
final
prefix → String
The folder inside driver this disk treats as its root.
final
readOnly → bool
Whether writes are refused.
final
runtimeType → Type
A representation of the runtime type of the object.
no setterinherited

Methods

allDirectories([String directory = '']) → Future<List<String>>
The paths of the folders in directory and all its sub-folders.
allFiles([String directory = '']) → Future<List<String>>
The paths of the files in directory and all its sub-folders.
append(String path, String text, {String separator = '\n'}) → Future<void>
Adds text to the end of the file at path, after separator when the file already has content. Creates the file if needed.
bytes(String path) → Future<Uint8List>
The file at path as bytes.
checksum(String path, {String algorithm = 'md5'}) → Future<String>
A hash of the file at path, as lowercase hex. algorithm is md5 (the default, as in Laravel), sha1, sha224, sha256, sha384 or sha512.
copy(String from, String to) → Future<void>
Copies the file at from to to.
copyToDisk(String disk, String path, [String? to]) → Future<void>
Copies the file at path to the same path on disk, or to to.
delete(Object paths) → Future<bool>
Deletes one path, or every path in an Iterable<String>.
deleteDirectory(String path) → Future<bool>
Deletes the folder at path and everything in it. Returns false when there was no folder.
directories([String directory = '']) → Future<List<String>>
The paths of the folders in directory.
directoryExists(String path) → Future<bool>
Whether a folder exists at path.
download(String url, String path, {ProgressCallback? onProgress, CancelToken? cancelToken, Map<String, dynamic>? headers, Map<String, dynamic>? queryParameters}) → Future<void>
Downloads url into path, replacing any file there only once the download has finished.
exists(String path) → Future<bool>
Whether a file exists at path.
files([String directory = '']) → Future<List<String>>
The paths of the files in directory.
get(String path) → Future<String>
The file at path as UTF-8 text.
json<T>(String path, {Map<Type, dynamic>? modelDecoders}) → Future<T?>
The file at path, decoded from JSON.
lastModified(String path) → Future<DateTime>
When the file at path last changed.
list([String directory = '', bool recursive = false]) → Future<List<DiskEntry>>
The files and folders in directory, with their size and date, or in all its sub-folders when recursive. Sorted by path.
makeDirectory(String path) → Future<void>
Creates the folder at path and any missing parents.
mimeType(String path) → Future<String?>
The MIME type of the file at path, from its first bytes and its extension, e.g. image/png. Null when neither says.
missing(String path) → Future<bool>
Whether no file exists at path.
move(String from, String to) → Future<void>
Moves the file at from to to.
moveToDisk(String disk, String path, [String? to]) → Future<void>
Moves the file at path to the same path on disk, or to to.
noSuchMethod(Invocation invocation) → dynamic
Invoked when a nonexistent method or property is accessed.
inherited
path(String path) → Future<String>
The absolute path of path on the device, for plugins that need a real file. Throws for disks with no files on the device, such as memory disks and the app bundle.
prepend(String path, String text, {String separator = '\n'}) → Future<void>
Adds text to the start of the file at path, before separator when the file already has content. Creates the file if needed.
prune({required Duration olderThan}) → Future<int>
Deletes the files last changed longer ago than olderThan, and returns how many were deleted.
put(String path, Object contents) → Future<void>
Writes contents to path: a String (as UTF-8), a List<int> of bytes, or a Stream<List<int>>. Replaces any file there.
putFile(String directory, Object file) → Future<String>
Stores file in directory under a random 40-character name, with the extension its contents show it to have, and returns the path.
putFileAs(String directory, Object file, String name) → Future<String>
Stores file in directory as name, and returns the path.
putJson(String path, Object? value) → Future<void>
Writes value as JSON. Models are encoded through their toJson().
readStream(String path) → Stream<List<int>>
The file at path, in chunks, without loading it all into memory.
scope(String directory) → Disk
A disk rooted at directory inside this one, e.g. one per user: disk().scope('users/${user.id}').
size(String path) → Future<int>
The size of the file at path, in bytes.
toString() → String
A string representation of this object.
inherited
url(String path) → Future<Uri>
A file:// URL for path, for web views and video players.
usage() → Future<int>
The total size of the files on this disk, in bytes.

Operators

operator ==(Object other) → bool
The equality operator.
inherited