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

Constructors

AssetPathResource(String path)
Represents a resource that can be loaded from a path-based asset.
factory
AssetPathResource.file(String path, [AssetLoaderInterface? assetLoader])
Represents a resource that can be loaded from a path-based asset.
factory

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

equalizedProperties() List<Object?>
Mixin-style contract for value-based equality, hashCode, and toString.
inherited
exists() bool
Checks whether the underlying resource exists.
get([Supplier<Exception>? throwIfNotFound]) Asset
Retrieves the asset represented by this resource.
getContentAsString() String
Example
inherited
getContentBytes() Uint8List
Returns the binary content of the asset as a Uint8List.
inherited
getFileName() String
Returns the name of the file represented by this asset.
inherited
getFilePath() String
Returns the full path to the file on disk or in the package.
inherited
getInputStream() InputStream
Returns a new InputStream to read the underlying content.
inherited
getPackageName() String?
Returns the name of the package from which this asset originates.
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 the base file name (without extension).
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 declaration into a JSON-compatible 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