AssetPathResource constructor

const AssetPathResource(
  1. String path
)

Represents a resource that can be loaded from a path-based asset.

Implementations of AssetPathResource provide ways to:

  • Check whether the resource exists.
  • Retrieve the asset or throw an exception if not found.
  • Safely attempt retrieval without forcing an exception.
  • Validate the file extension against a given list.

This is useful for handling project assets such as configuration files, templates, or bundled resources in a structured way.

Example

class ConfigFileResource extends AssetPathResource {
  final String path;

  const ConfigFileResource(this.path) : super(path);

  @override
  bool exists() => path.endsWith(".json");

  @override
  Asset get([Supplier<Exception>? throwIfNotFound]) {
    if (!exists()) {
      if (throwIfNotFound != null) throw throwIfNotFound();
      throw Exception("Asset not found: $path");
    }
    return this; // Example only
  }

  @override
  Asset? tryGet([Supplier<Exception>? orElseThrow]) =>
      exists() ? this : null;

  @override
  bool hasExtension(List<String> exts) =>
      exts.any((ext) => path.endsWith(ext));
}

Implementation

const AssetPathResource(String path);