Resources

A resource is a piece of content the application reads, wherever it lives, named by a location string such as resources:mail.txt.

Why use it

Code that reads a template, a data file or a config file should not care where the file is. With resources, the location string decides: resources: (the files of lib/resources/), file:, http: / https:, package:, or a prefix of your own. The code only reads.

lib/resources/ plays the role of the Java classpath. The generator embeds its files, so resources:... works in dart run, AOT executables, Flutter and the web, without asset declarations.

You do not need resources to read configuration values: use @Value or the Environment for that.

Example

import 'package:spring_core_dart/spring_core_dart.dart';

@Service()
class WelcomeLetter {
  // Resolved by the context's resource loader, so custom protocols and
  // asset loaders apply.
  WelcomeLetter(@Value('resources:templates/welcome.txt') this.template);

  final Resource template;

  Future<String> text() => template.readAsString();
}

Pages

See also