Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Bundling package assets is not described the way it works #11284

Open
1 task
Gustl22 opened this issue Oct 17, 2024 · 1 comment
Open
1 task

Bundling package assets is not described the way it works #11284

Gustl22 opened this issue Oct 17, 2024 · 1 comment
Labels
from.page-issue Reported in a reader-filed concern

Comments

@Gustl22
Copy link
Contributor

Gustl22 commented Oct 17, 2024

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:

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:

// lib/asset_example_package.dart

library asset_example_package;
import 'package:flutter/services.dart';
class AssetTestClazz {
  Future<String> getAssetStr() async {
    return await rootBundle.loadString('assets/my_asset.txt');
  }
}

Add these lines to the pubspec.yaml of the package:

flutter:
  assets:
    - assets/my_asset.txt

Replace the MyHomePage widget in the example/lib/main.dart with:

FutureBuilder(future: AssetTestClazz().getAssetStr(), builder: (context, snapshot) => Text(snapshot.data ?? 'not loaded yet'),)

Execute the example/main.dart

Expected fix

The docs state this:
https://docs.flutter.dev/ui/assets/assets-and-images#bundling-of-package-assets

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.

Related:
flutter/flutter#65921
flutter/flutter#111400

I would like to fix this problem.

  • I will try and fix this problem on docs.flutter.dev.
@Gustl22 Gustl22 added the from.page-issue Reported in a reader-filed concern label Oct 17, 2024
@Gustl22
Copy link
Contributor Author

Gustl22 commented Oct 17, 2024

Actually it worked by using within the package itself

    return await rootBundle.loadString('packages/asset_example_package/assets/my_asset.txt');

which is also decribed below in the docs:

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
from.page-issue Reported in a reader-filed concern
Projects
None yet
Development

No branches or pull requests

1 participant