We have the following structure:
Package A is a flutter app, It has it's assets folder and is loading assets like this: Image.asset("assets/images/background.png"). project B, that uses package A as dependency.
When I run package A from Project B, some widget of package A have image can not show. Error: Another exception was thrown: Unable to load asset: assets/images/light_theme_background.svg
When I added the "package" field to the AssetImage in the package A, Error is fixed when I run package A from project B, however the package A couldn't read its own contents.
const AssetImage('assets/heart.png', package: 'my_packages');
Is there a way to package A when it can read assets on run from itself and run from the project that takes it as a dependency?
Error when run package A: "Another exception was thrown: Unable to load assets: package/my_packages/assets/images/background.png"
In the documentation it says: "Content used by the package itself should also be fetched using the package argument as above."
I tried copying assets folder to lib folder and also declaring in pubspec.yaml file like the answer here. However, most reported "Bad state: Invalid SVG data " and "Exception: Invalid image data" errors even though they are all normal.
Related
As I was trying to change my package name using a package (change_app_package_name). I bumped into this error:-
Old Package Name: com.milanzi.shamba_huru
Updating build.gradle File
Updating Main Manifest file
Updating Debug Manifest file
Updating Profile Manifest file
ERROR:: Unknown Directory structure, both java & kotlin files not found.
Seems the package cannot find either java or kotlin files within the directory.
I have solved by changing the directory name where the MainActivity.kt is placed
For example
First your package name is com.example.app_name
Then you need to change into com.milanzi.shamba_huru (use your own package name)
Run
flutter pub run change_app_package_name:main com.milanzi.shamba_huru
So if the error appears, you just need to create a new folder by name of your package
Before your path was
/android/app/src/main/kotlin/com/example/app_name/MainActivity.kt
Change into
/android/app/src/main/kotlin/com/milanzi/shamba_huru/MainActivity.kt
Now try run against flutter pub run change_app_package_name:main com.milanzi.shamba_huru
NB: If you didn't change the folder name, you can't run it on android. But you can run on IOS.
I try to specify an entrypoint function in a specific file to launch a Flutter screen from my iOS APP, I follow the doc but get an error:
Dart Error: Dart_LookupLibrary: library 'hh.dart' not found.
This is my code in Xcode:
flutterEngine.run(withEntrypoint: "hhPage", libraryURI: "hh.dart")
and partial directory structure in the flutter module:
lib/
├─ main.dart
└─ hh.dart
The doc is not correct or out of date. According to the doc comment of libraryURI argument, we need to specify a string including its package name, like "package:your_package/hh.dart".
flutterEngine.run(withEntrypoint: "hhPage", libraryURI: "package:your_package_name/hh.dart")
Additionally, hh.dart has to be imported in main.dart to contain the file in the app.
I am trying to create a flutter package.
Inside Lib(package) folder, I have MY-PACKAGE.dart file. I also have a folder Called src which contains some codes I will import for use in MY-PACKAGE.dart.
In my pubspec.yaml in Example folder I have added the package like below and run packages get.
onboardly:
path: ../
To use my package I do this
// THERE IS NOT ANY PROBLEM WITH THIS IMPORT STATEMENT
import 'package:onboardly/onboardly.dart';
OnBoardly( // THIS CLASS WORKS FINE
screens: [
OnBoardlyScreenItem( // THIS CLASS CAN NOT BE FOUND EVEN THO IT EXISTS IN THE src FOLDER
image: Image.asset("assets/loadicon.png"),
description: Text("Hello There"),
),
],
),
The problem am facing is the fact that OnBoardlyScreenItem() which is in the src of the package can not be found.
I have ran
flutter packages get
flutter pub get
restarted my IDE
run flutter clean
Adding this for anyone later,
I had to export those classes or files that I wanted to be available to the Package to the "Entry file" since the folders in my case are regarded as private. Export all files or classes you'll want to be available to the user.
// Exporting all codes to be avaible to package
export 'package:onboardly/src/IntroScreen/OnBoardlyScreenItem.dart';
I have an module with an 'assets' folder located in the same directory as my pubspec.yaml file. In my assets folder I have test.txt, and simpleObject.json.
flutter:
assets:
- assets/test.txt
- assets/simpleObject.json
I believe the following code should then allow me to read it into my app.
var test = await DefaultAssetBundle.of(context).loadString("assets/test.txt");
Sadly I get the following error:
[ERROR:flutter/lib/ui/ui_dart_state.cc(148)] Unhandled Exception: Unable to load asset: assets/test.txt
The error comes from the asset_bundle.dart. I have to assume this is my fault, but according to everything I've read I'm doing it correctly. Any thoughts?
Here is my file structure if it helps.
MyModule
|_assets/test.txt
|_lib/
|_pubspec.yaml
I got a solution. Even though my package was trying to load its own asset, it still had to specify itself as the location.
Here is how my_package loads an image asset (specifying the package), uses it in a widget, and that widget is easily used by outside apps.
Image.asset(AssetImage("assets/splash.png").assetName, package: 'my_package',);
This is a bit late, but another solution can be:
Image.asset('packages/<package_name>/assets/splash.png');
I encountered this problem and took a lot of time before it was resolved.
The solution was to downgrade to a previous version (git checkout v1.5.4-hotfix.2)
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