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
If I create a package and want to include and use assets explicitely in that package, I cannot use them as described:
flutter create asset_example_package --template=package
cd asset_example_package
mkdir -p assets && echo "Hello World" > assets/my_asset.txt
# Create an app to test the package
flutter create example
This is what I expect to load an asset in a package itself:
If the desired asset is specified in the pubspec.yaml file of the package, it's bundled automatically with the application. In particular, assets used by the package itself must be specified in its pubspec.yaml.
A package can also...
Which is actually not applicable: the assets cannot be defined in the root of the package, as they cannot be accessed by the package itself, besides for testing. They must always be included in the lib directory. So it's not automatically bundled with the application, even if specified in the pubspec.yaml. So this section should be removed.
Edit: this actually works by using the prefix of packages/my-package. See below comment. It's just not clear that this approach must be used for both, referencing in the lib folder or referencing it in the root folder.
If I missed how they can be included inside a package, there should be also stated how this could be done (not from within the app facing package).
Additional context
Exuting the above example also on web doesn't show the asset in the package directory within the browser tools.
Edit: I cannot see the resource, but obviously its loading non the less.
To load the image within your package, use: return const AssetImage('packages/fancy_backgrounds/backgrounds/background1.png');
But this was not clear to me, that this approach also applies for the including the assets in the root (without using lib). So regardless, of using lib or placing it in the root, it's the same path.
You can close if this is not a reason to adapt the docs.
Page URL
https://docs.flutter.dev/ui/assets/assets-and-images/
Page source
https://github.com/flutter/website/tree/main/src/content/ui/assets/assets-and-images.md
Describe the problem
If I create a package and want to include and use assets explicitely in that package, I cannot use them as described:
This is what I expect to load an asset in a package itself:
Add these lines to the
pubspec.yaml
of the package:Replace the
MyHomePage
widget in theexample/lib/main.dart
with:Execute the
example/main.dart
Expected fix
The docs state this:
https://docs.flutter.dev/ui/assets/assets-and-images#bundling-of-package-assets
Which is actually not applicable: theassets
cannot be defined in the root of the package, as they cannot be accessed by the package itself, besides for testing. They must always be included in thelib
directory. So it's notautomatically bundled with the application
, even if specified in the pubspec.yaml. So this section should be removed.Edit: this actually works by using the prefix of
packages/my-package
. See below comment. It's just not clear that this approach must be used for both, referencing in thelib
folder or referencing it in the root folder.If I missed how they can be included inside a package, there should be also stated how this could be done (not from within the app facing package).Additional context
Exuting the above example also on web doesn't show the asset in the package directory within the browser tools.Edit: I cannot see the resource, but obviously its loading non the less.
Related:
flutter/flutter#65921
flutter/flutter#111400
I would like to fix this problem.
The text was updated successfully, but these errors were encountered: