Resources class abstract final

The registry behind resources: locations: the files of lib/resources/ of the application and of its dependencies.

Dart has no classpath, so the generator embeds every file of a package's lib/resources/ into its generated module, and the files are registered here when the module is created. That is how application.yml, message bundles and templates are found on every platform. A path that is not embedded (a file added while developing without regenerating) is read from lib/resources/<path> on disk. Directories added with addDirectory come first, like src/test/resources before src/main/resources on a test classpath: a file there shadows the embedded file with the same path. A path whose .. segments climb above lib/resources/ (resources:../../secret) finds nothing, as Java's class loader refuses such a name: resolve gives a resource that does not exist.

Applications only write locations (resources:templates/mail.txt); resource loaders call this class. Code calls it directly in tests, to add a directory, or to list the embedded files.

// Reads lib/resources/banner.txt, embedded or from disk.
final resource =
    DefaultResourceLoader().getResource('resources:banner.txt');
log.info(resource.readAsStringSync());

Spring (Java):

Resource resource = new ClassPathResource("banner.txt");

Properties

hashCode → int
The hash code for this object.
no setterinherited
runtimeType → Type
A representation of the runtime type of the object.
no setterinherited

Methods

noSuchMethod(Invocation invocation) → dynamic
Invoked when a nonexistent method or property is accessed.
inherited
toString() → String
A string representation of this object.
inherited

Operators

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

Static Properties

directories → List<String>
The directories searched before the embedded files, in search order.
no setter
paths → Iterable<String>
The paths of all embedded files, relative to lib/resources/.
no setter
roots → List<ResourceRoot>
The roots added with addRoot, in search order.
no setter

Static Methods

addDirectory(String directory) → void Function()
Makes resources: look in directory on disk before the embedded files, until the returned function is called. The most recently added directory comes first. spring_boot_starter_test uses it for test/resources/, so test config files win over packaged ones.
addProtocolRoot(String prefix, ResourceRoot root) → void Function()
Makes the patterns of the protocol prefix (asset:) list the files of root (asset:docs/**/*.md), until the returned function is called; without one, a pattern of another protocol finds the one location as written.
addRoot(ResourceRoot root) → void Function()
Makes resources: look in root for a path that is not embedded, before lib/resources/ on disk, until the returned function is called: a file the build left out of the binary (spring.resources.embed.exclude) is then still resources:<path>. The Flutter starter adds the app's Flutter assets below lib/resources/, which the web downloads when a file is read.
normalize(String path) → String?
path relative to lib/resources/ (without resources:): a leading / removed, \ read as /, . and empty segments dropped, .. resolved (/config/./app.properties gives config/app.properties). null when .. climbs above the root: nothing there is on the classpath.
protocolRootOf(String location) → (ResourceRoot, String)?
The root of the protocol of location added with addProtocolRoot, and the path after its prefix; null when none.
registerAll(Map<String, EmbeddedResource> resources, {String? owner}) → void
Registers embedded files, keyed by their path under lib/resources/. Generated modules call it; applications do not.
resolve(String path) → Resource
The resource for resources:<path> or a plain path: a file of an added directory when it exists there, otherwise the embedded file, otherwise the file under directory (which may not exist). A path that climbs above the root with .. gives a resource that does not exist and cannot be read; so does createRelative of an embedded file. A file read from disk is a FileSystemResource, whose createRelative is a file's.
resolveEmbedded(String path) → Resource
The resource for path, ignoring the added directories: the embedded file, or else the file under directory; nothing for a path that climbs above the root, as in resolve.

Constants

allPrefix → const String
The prefix that searches every root, resources*: (Spring's classpath*:).
directory → const String
The directory read from disk for paths that are not embedded: lib/resources, relative to the working directory.
prefix → const String
The location prefix, resources:.