UrlResource class

A Resource on a web server, read with an HTTP GET request.

http: and https: locations resolve to it, so configuration can be imported from a config server (spring.config.import=https://...) and @PropertySource can name a URL. Applications get it from a resource loader; they rarely create one.

Reading is asynchronous only (supportsSyncRead is false), so such imports need an asynchronous start (runAsync, Flutter). A status of 400 or above fails the read. exists cannot ask the server synchronously and is always true; existsAsync sends the HEAD request Spring's exists() sends, so a 404 means not found. Config data (optional:https://...) and @PropertySource (ignoreResourceNotFound) check it before reading. Reading needs dart:io, so it does not work on the web.

final resource = DefaultResourceLoader()
    .getResource('https://example.com/config/app.properties');
final text = await resource.readAsString();
final sibling = resource.createRelative('app-dev.properties');

Spring (Java):

Resource resource = new UrlResource("https://example.com/config/app.properties");
String text = resource.getContentAsString(StandardCharsets.UTF_8);
Inheritance

Constructors

UrlResource(Uri url)
Creates the resource for url; nothing is requested yet.
const

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 setteroverride
filename → String?
The last part of the path, such as application.yml, or null when the resource has no file name (bytes in memory).
no setteroverride
hashCode → int
The hash code for this object.
no setterinherited
isReadable → bool
Whether the content can be read; usually the same as exists, but false for a directory.
no setterinherited
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 setterinherited
uri → Uri
The URI of the resource (file:, https:, ...), or null when it has none.
no setteroverride
url → Uri
The URL that is requested.
final

Methods

createRelative(String relativePath) → Resource
The resource at relativePath resolved against this URL, such as a profile variant next to a config file. A leading / is ignored, as in Spring, so the path stays relative to this URL's directory.
override
exists() → bool
Always true, because the server cannot be asked synchronously; use existsAsync to find out.
override
existsAsync() → Future<bool>
Asks the server whether the resource is there: a HEAD request (a GET when the server rejects HEAD) answered below 400, like Spring's exists(). A 404 or a connection error means it does not exist. Always true on the web, where reading then fails.
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.
override
readAsBytesSync() → Uint8List
Reads the whole content as bytes without waiting, for code that cannot be asynchronous. Throws UnsupportedError unless supportsSyncRead.
inherited
readAsString({Encoding encoding = utf8}) → Future<String>
Reads the whole content as text, decoded with encoding (UTF-8 unless given).
inherited
readAsStringSync({Encoding encoding = utf8}) → String
Reads the whole content as text without waiting, decoded with encoding. Throws UnsupportedError unless supportsSyncRead.
inherited
toString() → String
A string representation of this object.
inherited

Operators

operator ==(Object other) → bool
Whether other is a resource of the same run-time type with the same description, as in Spring.
inherited