DefaultResourceLoader class
The ResourceLoader used everywhere: it knows resources:, file:,
http:, https: and package: locations, plus any prefix taught by a
ProtocolResolver.
The application context uses one unless given another loader, so applications mostly use it through the context. Create one directly to read resources without a context, in a tool or a test. Locations are handled in this order:
- Registered ProtocolResolvers, in order.
resources:: the files oflib/resources/(see ClasspathResources).file:: a FileSystemResource, percent-decoded (see FileSystemResource.fromFileUrl).http:andhttps:: a UrlResource.package:: the package's file, through the package config of the running program; not on the web or in AOT executables.- Any other
scheme:: a resource that does not exist. - A plain path (
app.properties,/config/x.txt): a classpath resource, like Spring'sgetResourceByPath.
Platforms with their own asset system plug in with a
ProtocolResolver, which comes first: a Flutter app can resolve
package:my_app/config/app.properties (or an asset: scheme) to a
resource over its asset bundle that is read asynchronously, which
ConfigDataEnvironment and @PropertySource support.
final loader = DefaultResourceLoader()
..addProtocolResolver(InMemoryProtocolResolver('memory:', {}));
loader.getResource('config/app.properties'); // lib/resources/config/...
loader.getResource('file:///etc/app.properties'); // FileSystemResource
loader.getResource('https://example.com/x'); // UrlResource
loader.getResource('ftp://example.com/x').exists(); // false
Spring (Java):
var loader = new DefaultResourceLoader();
loader.addProtocolResolver(myResolver);
Resource onClasspath = loader.getResource("config/app.properties");
Resource file = loader.getResource("file:config/app.properties");
- Implemented types
Constructors
-
DefaultResourceLoader({Iterable<
ProtocolResolver> protocolResolvers = const []}) -
Creates a loader that asks
protocolResolversfirst, in order, before its built-in rules.
Properties
- hashCode → int
-
The hash code for this object.
no setterinherited
-
protocolResolvers
→ List<
ProtocolResolver> -
The resolvers asked before the built-in rules, in order, as a
read-only copy.
no setter
- runtimeType → Type
-
A representation of the runtime type of the object.
no setterinherited
Methods
-
addProtocolResolver(
ProtocolResolver resolver) → void -
Adds
resolverafter the resolvers already registered. -
getResource(
String location) → Resource -
The resource at
location. Nothing is read yet, and the resource may not exist: check Resource.exists before reading when it may be missing. Never returnsnull; an unknown prefix gives a resource that does not exist.override -
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
Constants
- packagePrefix → const String
-
The prefix of a Dart package URI,
package:, as inpackage:my_app/config/app.yml.