You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
extensiononBuildOutput {
/// Adds all the files in [directory] as [DataAsset]s to this [BuildOutput]. /// /// The [directory] must end with a slash and be relative to /// [BuildConfig.packageRoot].Future<void> addDataAssetsDirectory(
String directory, {
requiredBuildConfig config,
String? linkInPackage,
}) async {
final packageName = config.packageName;
final packageRoot = config.packageRoot;
final assetDirectory =Directory.fromUri(packageRoot.resolve('assets/'));
addDependency(assetDirectory.uri);
awaitfor (final assetFile in assetDirectory.list()) {
if (assetFile is!File) {
continue;
}
final assetUri = assetFile.uri;
// The file path relative to the package root, with forward slashes.final name = assetUri
.toFilePath(windows:false)
.substring(packageRoot.toFilePath(windows:false).length);
addAsset(
DataAsset(
package: packageName,
name: name,
file: assetUri,
),
linkInPackage: linkInPackage,
);
addDependency(assetUri);
}
}
}
With such an implementation we'd also establish a convention that the a data asset in assets/foo.json will be available at runtime as package:my_package/assets/foo.json which aligns with the Flutter assets approach of making assets available via their full path.
In would be even nicer if adding all assets was enabled by default. This requires us to land automatic treeshaking of assets first, and needs special care because of existing asset folders.
Does not yet create a helper function as suggested in #1346, but does standardize the asset names to be the file path.
@mosuem I don't believe this should break https://dart-review.googlesource.com/c/sdk/+/351140, as the asset id is part of the `.dart.debug` files and I updated it there.
Adding all files in a directory as data assets seems to be a common use case.
In Flutter, all files in a directory can be added as assets easily:
https://docs.flutter.dev/ui/assets/assets-and-images#specifying-assets
Using these assets in Flutter is via the full file path:
https://docs.flutter.dev/ui/assets/assets-and-images#loading-text-assets
We should be able to support this type of use case:
Implementation sketch:
With such an implementation we'd also establish a convention that the a data asset in
assets/foo.json
will be available at runtime aspackage:my_package/assets/foo.json
which aligns with the Flutter assets approach of making assets available via their full path.Inspired by #1345 (comment).
Open to alternative suggestions, WDYT @mosuem?
The text was updated successfully, but these errors were encountered: