A piece of content the application reads, wherever it lives: a file of
lib/resources/ (the classpath), a file on disk, a URL, a Flutter
asset, bytes in memory.
It lets code read a template, a data file or a config file without
caring where it comes from: the location string (resources:mail.txt,
file:./data.csv, https://...) decides, and the code only reads.
Applications receive resources: a @Value('resources:...') parameter
of type Resource, or getResource on the application context (any
ResourceLoader). They implement it only for a new kind of storage,
usually by extending AbstractResource.
Reading is asynchronous because some sources (Flutter assets, network) can only be read that way. Resources that can also be read synchronously say so in supportsSyncRead; the synchronous readers of the others throw UnsupportedError.
@Component()
class OrderMailer {
// The template ships in lib/resources/templates/, embedded by the
// generator, so it is found on the server, in Flutter and on the web.
OrderMailer(
@Value('resources:templates/order-confirmation.txt') this.template,
);
final Resource template;
Future<String> body() async => (await template.readAsString()).trim();
}
Spring (Java):
@Component
class OrderMailer {
private final Resource template;
OrderMailer(@Value("resources:templates/order-confirmation.txt")
Resource template) {
this.template = template;
}
String body() throws IOException {
return template.getContentAsString(StandardCharsets.UTF_8).trim();
}
}
Spring's getInputStream() and getFile() have no counterpart; read
the whole content instead.
Properties
- description → String
-
A text that tells people which resource this is, for error messages,
such as
file [/app/config/application.properties]. It is also the default name of a property source read from it, and two resources of the same type with the same description are equal.no setter - filename → String?
-
The last part of the path, such as
application.yml, ornullwhen the resource has no file name (bytes in memory).no setter - hashCode → int
-
The hash code for this object.
no setterinherited
- isReadable → bool
-
Whether the content can be read; usually the same as exists, but
falsefor a directory.no setter - runtimeType → Type
-
A representation of the runtime type of the object.
no setterinherited
- supportsSyncRead → bool
-
Whether readAsBytesSync and readAsStringSync work. True for files
and the embedded classpath; false for URLs and Flutter assets.
no setter
- uri → Uri?
-
The URI of the resource (
file:,https:, ...), ornullwhen it has none.no setter
Methods
-
createRelative(
String relativePath) → Resource -
The resource at
relativePathfrom this one, such as a sibling file next to a config file. It is not checked to exist. Throws UnsupportedError for resources without a location (bytes in memory). -
exists(
) → bool -
Whether the content is there: the file exists, the classpath entry is
embedded. Check it before reading a resource that may be missing.
A
UrlResourcecannot ask the server synchronously and always saystrue. -
noSuchMethod(
Invocation invocation) → dynamic -
Invoked when a nonexistent method or property is accessed.
inherited
-
readAsBytes(
) → Future< Uint8List> - Reads the whole content as bytes. Works for every resource that exists; fails for one that does not.
-
readAsBytesSync(
) → Uint8List - Reads the whole content as bytes without waiting, for code that cannot be asynchronous. Throws UnsupportedError unless supportsSyncRead.
-
readAsString(
{Encoding encoding = utf8}) → Future< String> -
Reads the whole content as text, decoded with
encoding(UTF-8 unless given). -
readAsStringSync(
{Encoding encoding = utf8}) → String -
Reads the whole content as text without waiting, decoded with
encoding. Throws UnsupportedError unless supportsSyncRead. -
toString(
) → String -
A string representation of this object.
inherited
Operators
-
operator ==(
Object other) → bool -
The equality operator.
inherited