How can I run an example from a Flutter package/library? - flutter

I have the following simple directory structure:
flutter_published
.idea
android
build
ios
lib
main.dart
flutter_published.iml
pubspec.lock
pubspec.yaml
network_to_file_image
.idea
example
main.dart
lib
network_to_file_image.dart
test
network_to_file_image.iml
pubspec.lock
pubspec.yaml
network_to_file_image is a package.
There are two main.dart files, one at flutter_published/lib/main.dart and
another at flutter_published/network_to_file_image/example/main.dart
I am able to run the first one, but not the one inside of the example directory under network_to_file_image. The second one gives me this error:
Launching example\lib\main.dart on Android SDK built for x86 in debug mode...
No application found for TargetPlatform.android_x86.
Is your project missing an android\AndroidManifest.xml?
Consider running "flutter create ." to create one.
Also, when the app is generated, what happens to the example and test directories of the packages I use? Are they included or removed from the final app that is deployed?

To solve this, instead of the main.dart file inside of the example directory, you need to create a complete Flutter application-type project inside of the example directory. Then, the example tab will point to the README.md file inside of that directory.
That example directory will have its own lib directory, containing a main.dart file. Since that file is now inside of an application-type directory it can be run.
Visit this repo to see how it works:
https://github.com/marcglasberg/async_redux/tree/master/example
Update:
To be clear, the example's pubspec.yaml file can reference its package by using a relative reference. For example, here is the dependencies section of the example dir of the async_redux package I mentioned:
dependencies:
http: ^0.13.1
async_redux:
path: ../
flutter:
sdk: flutter
Since the example dir is at the same level as the package's pubspec.yaml file, then the example's own pubspec.yaml is one level below it. Thus, it may reference the package itself by using a ../ path:
async_redux:
path: ../

example/main.dart only exists to be shown in https://pub.dartlang.org/packages/network_to_file_image#-example-tab-
The pub site is limited in how it finds content in the example directory to display in the Example tab.

cd to the directory and execute flutter create .. You should be able to run it afterwards

Go to the example folder of the repository of the package.
Open the lib folder and go to the main.dart file.
Above the void main() function, you can see the Run|Debug|Profile (pic below), click on the one you want to run the project as.
The example app will now run in your emulator.
The screenshot below is of the chewie package available on pub.dev

Related

Flutter project not picking up local flutter package updates

Flutter 3.3.9
I created a flutter project and I reference it as a dependency in another flutter project like so:
dev_dependencies:
flutter_test:
sdk: flutter
my_utils:
path: ../my_utils
When I added the local package to my project initially, I could see and debug the referenced local package just fine. I made updates to the my_utils package, did a "flutter pub get" in the project referencing my_utils, and the changes are not being picked up.
I added a new class to my_utils and it is not finding it in the other project.
I have this in analysis_options.yaml:
include: package:flutter_lints/flutter.yaml
linter:
rules:
depend_on_referenced_packages: false
Setting depend_on_referenced_packages to true didn't help.
How do I make my changes/updates in my_utils show in my referencing project?
Thanks
did you export the files properly ? e.g files inside src/?
my_utils file structure should be like the following:
lib/
...src/
......my_impl.dart
...my_utils.dart
and in your my_utils.dart should contain the following:
export 'src/my_impl.dart';
I recommend you use melos for managing multi-package projects. it might solve your problem. Also with melos bootstrap, you get flutter pub get running in all packages with one command.
you can follow the installation from here

flutter pubspec.yaml deleted after updating

Hello experts I am facing very big issue I am made application in 2021 on flutter my application is still live on play store now I need update it, problem is that I updated my android studio flutter version 3.0. Now opening my old project which was made in 2021 after opening it my pubspec.yaml file is missing in the project, I don't no how to recover that file or repair it. They have a lot of dependencies included there is pubspec.lock file available but not pubspec.yaml please please help me i am in big check screen shot below
if i use flutter create command i am getting this errors
You can create a pubspec.yaml file on root directory or open the bottom terminal and try flutter create . it will provide default structure. Now to get pub package, check the import section and add on pubspec.yaml
If you have a backup then paste the pubspec.yaml file from it. Else take a backup of the project. Run flutter create . in the root directory. It will create the pubspec file in the project. Click on configure dart and add the flutter path then run the project. It will throw errors for the packages used manually enter packages in pubspec or you can try flutter pub add <packagename>

The file "assets/background.png" set as the parameter "image" was not found. pub finished with exit code 1

I am trying to create a splash screen using flutter_native_splash 1.3.1 package. I created a folder called 'assets' in the lib folder and added that image to that folder. But I am getting the error mentioned in the question when I ran 'flutter pub run flutter_native_splash:create' in the command line.
I added the following lines in pubspec.yaml as suggested by the package documentation.
flutter_native_splash:
color: "#000000"
image: assets/background.png
Can someone please explain what I might be missing?
Do the following steps
Add flutter_native_splash: ^1.3.1 in pubspec.yaml
Create flutter_native_splash.yaml in root of project at same level as pubspec.yaml
Add below content in the above file
flutter_native_splash:
color: "#000000"
image: assets/background.png
Run this command in terminal
flutter pub run flutter_native_splash:create
With above steps, splash screen will load properly but the error you mentioned might come. To remove that error enable below section in pubspec.yaml
assets:
- assets/background.png
6.Make sure you have assets directory in the root of project and background.png is present inside it.
7.Clean and build. it should work without any issues
Continuing the Ehtesham Siddiquie answer.
I think the assets folder should be at the root of the project (at the same level of pubspec.yaml and flutter_native_splash.yaml). but u created it in the lib folder.

What is packages in Flutter assets inside pubspec.yaml file?

I'm using flutter gallery sample code, and found following in pubspec.yaml file.
flutter:
assets:
- packages/shrine_images/0-0.jpg
I tried to find out packages and 0-0.jpg file in the project but couldn't find. Can anyone tell me what this package is all about and from where I am seeing images when running the app?
Actually they have added a package
flutter_gallery_assets: 0.1.9+2
Check it in pubspec.yaml dependencies, its a package so that you can use images from this package. So the pictures are from that package and they are just mentioning the image path from that package. Here is the package link they are using click here

I am not able to use other packages in dependencies in flutter

I have just started using flutter and I am using flutter packages like material.dart , cupertino.dart but when it come to use other packages like image_picker, english_words, I am not able to use it and I had declared packages with versions in dependencies in pubsec.yaml file. After when I write the import package name in the main file it shows a red line under it and the folder was also not created on the left side.
It seems the packages that were declared as dependencies in pubspec.yaml files are not yet downloaded.
Try running: flutter packages get from your project's root directory.
Sometimes it doesn't work even after the above command is executed, in such case close current project and reopen it.