AssetPathResource class abstract

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));
}
Implemented types
Implementers
Available extensions

Constructors

AssetPathResource(String path)
Represents a resource that can be loaded from a path-based asset.
const

Properties

hashCode int
The hash code for this object.
no setterinherited
runtimeType Type
A representation of the runtime type of the object.
no setterinherited

Methods

exists() bool
Checks whether the underlying resource exists.
get([Supplier<Exception>? throwIfNotFound]) Asset
Retrieves the asset represented by this resource.
getContentAsString() String

Available on Asset, provided by the AssetExtension extension

Extension methods for Asset objects.
getContentBytes() Uint8List
Returns the binary content of the asset (same as _contentBytes).
inherited
getFileName() String
Returns the name of the file (same as _fileName).
inherited
getFilePath() String
Returns the full path to the file (same as _filePath).
inherited
getInputStream() InputStream
Returns a new InputStream to read the underlying content.
inherited
getPackageName() String?
Returns the name of the originating package (same as _packageName).
inherited
getResourcePath() String
Returns the underlying file system path for this resource.
getUniqueName() String
Returns a unique name for the asset, combining the package name and file name.
inherited
hasExtension(List<String> exts) bool
Checks whether the asset’s path ends with any of the provided extensions.
noSuchMethod(Invocation invocation) → dynamic
Invoked when a nonexistent method or property is accessed.
inherited
toJson() Map<String, Object>
Serializes this resource to a JSON-encodable map.
inherited
toString() String
A string representation of this object.
inherited
tryGet([Supplier<Exception>? orElseThrow]) Asset?
Attempts to retrieve the asset safely.

Operators

operator ==(Object other) bool
The equality operator.
inherited