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:

  1. Registered ProtocolResolvers, in order.
  2. resources:: the files of lib/resources/ (see ClasspathResources).
  3. file:: a FileSystemResource, percent-decoded (see FileSystemResource.fromFileUrl).
  4. http: and https:: a UrlResource.
  5. package:: the package's file, through the package config of the running program; not on the web or in AOT executables.
  6. Any other scheme:: a resource that does not exist.
  7. A plain path (app.properties, /config/x.txt): a classpath resource, like Spring's getResourceByPath.

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 protocolResolvers first, 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 resolver after 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 returns null; 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 in package:my_app/config/app.yml.