How to include file assets to be used by your own package in flutter - flutter

I have a package where I'm trying to load a string from my rootBundle. This file is to be used within the package itself only as part of internal configuration. I cant see where i'm going wrong.
This is my folder structure:
my-package
--> lib
--> pubspec.yaml
--> assets/file.js
This is the content of my pubspec.yaml
flutter:
assets:
- assets/
- assets/file.js
This is how i'm calling it
String js = await rootBundle.loadString('assets/file.js');
I keep on getting unable to load asset
Unable to load asset: assets/file.js

had the same problem but solved it by doing this:
In your package's .yaml file, add this line to import the file/asset
assets:
- packages/<Package name>/assets/<File name>
Replace the <Package name> with the name of your package, and <File name> with the name of said file/asset.
Then place the file/asset in a folder under your lib directory of your package. The structure should look something like this:
my_package
--> lib
--> assets
file.js
Make sure to add any additional folders to the reference if necessary.
When referencing the file/asset in your app or package, use the same reference as in you .yaml file:
String js = await rootBundle.loadString('packages/<Package name>/assets/file.js');
For example, here I'm getting a .js file.

Related

ObjectBox generator warning:"Failed to find package root from output directory, generated imports might be incorrect."

Iam using the latest version of objectbox and when i run the "flutter pub run build_runner build" command i get this output: "...Failed to find package root from output directory, generated imports might be incorrect..."
So when i write in main to init ObjectBox:
late ObjectBox objectBox;
ObjectBox is not recognized with import.
Using the import from an tutorial which have the same version does not solve the problem,but the json and g.dart files are generated.
What iam doing wrong?
Let me know if you need more information!
Thanks
You will get the error you are seeing if your ObjectBox files are in the wrong directory. By default, ObjectBox expects objectbox.dart, objectbox.g.dart, and objectbox-model.json to be at the root of your project (i.e. in the lib directory next to the main.dart file. You can move objectbox.dart though. I typically put it in lib/objectbox, but I think that objectbox.dart and the generated files need to exist in the same directory, though I'm not 100% sure about that. Change the directory where objectbox.g.dart and objectbox-model.json are generated by following the instructions below from the ObjectBox README.md. If you wanted to put the files in lib/objectbox you would just change custom to objectbox in the code snippet below:
To customize the directory (relative to the package root) where the
generated files are written, add the following to your pubspec.yaml:
objectbox:
# Writes objectbox-model.json and objectbox.g.dart to lib/custom (and test/custom).
output_dir: custom
# Or optionally specify the lib and test output folder separately.
# output_dir:
# lib: custom
# test: other

Flutter: Package can't load a string resource using rootBundle.loadString - asset indenting correct

I have a project that consumes another package on the drive using a relative path:
project1 (setup as a full flutter project with flutter create project1)
project2 (setup with flutter create --template=package
project1's packages.yaml does this:
dependancies:
project1:
path: ../project2
project2's packages.yaml does this:
flutter:
assets:
- lang/en.json
Which works and everything sees everything else and there is no complaint about that path for the asset and I've verified that it has exactly 2 spaces before assets: and exactly 4 actual spaces beofre - lang/en.json
The problem occurs when project2 tries to load lang/en.json like this in code form project2:
final jsonString =
await rootBundle.loadString('lang/en.json');
I get an "asset could not be loaded ${key}" on the loadString function.
if however I take exactly the same code and put it on project1 and copy the folder exactly and copy the exact same asset tag in packages.yaml, project1 has no problem loading the file. If I even leave the asset links on the project1 then project2 can load them just fine too.
Is this a bug, or am I doing something wrong with the package template version?
I have same issue. and I found the solution:
In project2, do some steps as below:
create assets folder
create lang folder(or any other folder name)
create json file in lang folder. Ex: en.json, vi.json...
in pubspec.yaml of project, you need to declare assets:
flutter:
assets:
- assets/lang/vi.json
- assets/lang/en.json
when using loadString then the path will be :
await rootBundle.loadString('packages/language_pack/assets/langen.json');
Note that packages is plural and language_pack is package name

Target URI doesn't exist, when trying to import a package in flutter

I have stumbled upon a problem in importing the package in Flutter, I tried to solve this by running flutter packages get and also shutting down the project in Android studio and reopening it.
import 'package:task_02_category_widget/category.dart';
Here is the line above, and the error I'm running into when I run it gives the following error in the console.
Your application could not be compiled, because its dependencies could
not be established.
The following Dart file:
/Users/username/Documents/flutter_rectangle_2/lib/main.dart
...refers, in an import, to the following library:
package:task_02_category_widget/category.dart
That library is in a package that is not known. Maybe you forgot to
mention it in your pubspec.yaml file?
If task_02_category_widget/category.dart is part of an old project you are reusing you should put it in a folder in your flutter application and include it like "../ folder /task_02_category_widget/category.dart ". If it is part of github repository you have copy pasted from, just copy the file and use the step above. Most probably you are looking for that . In any other case check here to find the source code.
You should have in your project at a file called pubspec.yaml a definition like this:
name: my_app
dependencies:
task_02_category_widget:
Let’s say that your package is laid out as follows:
task_02_category_widget/
lib/
category.dart
Then, you can import it:
import 'package:task_02_category_widget/category.dart';
More information:
https://www.dartlang.org/tools/pub/get-started
https://www.dartlang.org/guides/libraries/create-library-packages

Multiple assets in Flutter project

According to documentation to include image assets in a Flutter project, I need to add them to the pubspec.yaml like this:
flutter:
assets:
- assets/my_icon.png
- assets/background.png
But I have ~900 images that I need to include in my application, do I really need to manually include one by one in the file?
Sadly, you can't import the whole assets/ folder at once but you can now import all the assets inside a folder.
# ❌ Don't Import every files inside assets folder, its tiring ❌
assets:
- assets/audio/celebrate.mp3
- assets/audio/splash.mp3
- assets/lottie/paper_plane.json
- assets/lottie/celebration.json
- assets/images/png/stats.png
- assets/images/alert.svg
# ❌ Import all assets at once,🚨 Not allowed ❌
assets:
- assets/
# βœ… Just import every folders inside assets βœ…
assets:
- assets/lottie/
- assets/images/svg/
- assets/images/png/
- assets/audio/
This is no longer a restriction. You can now include all assets in a directory.
In the flutter docs it states:
To include all assets under a directory, specify the directory name with the / character at the end:
flutter:
assets:
- directory/
- directory/subdirectory/
Yes, currently this is required and a known limitation.
Wildcarding assets in pubspec.yaml (or allow an entire directory to be included) #4890
You could use code generation to get the assets listed all at once,
but you'll also get into troubles when the list becomes too long.
The flutter development tool named Flr(Flutter-R) can help you to auto specify image/text/font assets in pubspec.yaml and generate r.g.dart file.
More details see: https://stackoverflow.com/a/61200175/1505549

Dart, SASS: importing files located in the web/ directory

I have created a general.scss file and placed it under the web/config folder. And I would like to import that file from any other SASS files. But the following script doesn't work:
// anyotherfile.scss
#import 'web/config/general';
The previous command throws the following error:
Build error: Transform Sass on axis_mobile|lib/app_router.scss threw
error: could not resolve import 'Instance of '_SassImport'' (tried
[axis_mobile|lib/config/general.scss,
axis_mobile|lib/config/general.sass,
axis_mobile|lib/config/_general.scss,
axis_mobile|lib/config/_general.sass]) null
How could I import a file located under the web/ directory?
You shouldn't import from web/, at least not if the importing file is outside web/.
Files that are supposed to be imported from different directories got to lib/ or one of its subdirectories.
For files in /lib there is always a canonical path using the packages/mypackage/somefile that points to the lib/somefile of mypackage where mypackage is the name of the package in the pubspec.yaml file of the current project.
This way also files can also be imported if the project is a dependency of another project while other directories are not supported.