How to add flutter packages to pubspec.yaml automatically in vscode - flutter

How do I flutter packages to the pubspec.yaml file without going directly to the pubspec.yaml file.
I want to be able to add packages without leaving the current page I am working on.
I have seen this done in several tutorials and I want to know how it is done.

There are two ways of doing auto import in vscode:
Setting up Pubspec assist in VScode
Using the terminal (this does not require any extension)
1. To setup in Pubspec Assist vscode use the instructions below:
Install Pubspec assist extension in Vscode
Then go to the command panel using the shortcut [Ctrl+Shift+P on Windows OR ⌘+Shift+P on Mac](without the '+')
On the text field type - Pubspec Assist: add/update dependencies
Enter the name of the package you want to find
Nb:
You install multiple packages once by separating them with commas
eg: bloc, provider, http
You only need to go through process 2 to 5 whenever you need to install a package again
hope this helps😊
2. To use Terminal
Go to the terminal and type: flutter pub add <package name>
eg: flutter pub add bloc
To upgrade a package use: flutter pub upgrade <package name><br>
eg. flutter pub upgrade bloc
this second method does not bring up a dialog but also does the same auto import
Have a nice day 👋

Related

Project files can't see library

There are many libraries I use every day from pub and they work. But today I run into a problem with qr_code_scanner. After installing it with flutter pub add qr_code_scanner and running flutter pub get, I get in code:
Same with auto import for classes from this file like QRViewController. What I also did is running flutter upgrade, flutter clean and everything else that came to my mind - still getting this error with that package.
Any other thoughts how can I solve it?
Some of the packages you are using are upgraded to flutter3 and some are still Flutter version < 3. Which is why you might be facing an issue. If you are trying to build an older project I would recommend you to downgrade your package versions which is compatible with flutter < 3 and use flutter version < 3. If you are adding any packages through pub add you might get an error if the pub get is not resolved. Please check the same in terminal.
For anyone who will have the same issue, I found a workaround. I've also tried few other libraries that beside the fact was installed couldn't access them from anywhere in code.
So after all every pub.dev lib is just the code right? You can download the source file and add it into your lib folder. Well, you have to upgrade it manually if needed but at the end it works and that's the main goal!
EDIT:
Another possible solution, as I found out, I did reset my Mac, upgraded environment (VS Code), done flutter clean and flutter pub get and looks like it's working as it should without mentioned workaround so probably the issue was around the coding environment rather than with Flutter and fetched libraries.

Why `Flutter Doctor` and `New Application Project` fails in my VSCode setup?

Following these directions, like:
Install the Flutter and Dart plugins
Start VS Code.
Invoke View > Command Palette….
Type “install”, and select Extensions: Install Extensions.
Type “flutter” in the extensions search field, select Flutter in the list, and click Install. This also installs the required Dart plugin.
Validate your setup with the Flutter Doctor
Invoke View > Command Palette….
Type “doctor”, and select the Flutter: Run Flutter Doctor.
Review the output in the OUTPUT pane for any issues. Make sure to select Flutter from the dropdown in the different Output Options.
Step 3 of validate instructions results with OUTPUT:
I did install flutter, and flutter doctor in a new command shell gives result:
What am I missing here? I also try VSCode extension palette command Flutter: New Application Project and that also fails with:
Make sure that you have installed Git and that you can access it from the command line. Check your PATH. Flutter SDK directory should be present there.
If that does not work, you should try to reinstall the dart and flutter extension.
Next Step should be to reinstall flutter.
It occurred to me that my attempt to support multiple flutter versions might be going outside of the conventions supported by the Flutter extension for VSCode.
Here's what's up!... If you think you're smart like me and installed different versions of flutter in folders like c:\src\flutter\1.20.2 and c:\src\flutter\1.22.6, then you can forget it because the extension is smarter than us, and will ignore this regardless of our PATH assignment.
Copying contents of my flutter install from c:\src\flutter\1.22.6 to the parent folder c:\src\flutter resolved my issue. I need to review how to easily support different versions of flutter. Hmmm..
BTW... what I realized in retrospect is that the complaint about git is raised by the extension not finding the .git folder under the flutter folder (i.e. c:\src\flutter) that it presumed as the path to my flutter install. Well duh!! That's because I want flutter to be sourced at the versioned sub-folder (i.e. c:\src\flutter\1.22.6). Oh well.
Some scenarios I am left pondering:
How to correctly support multiple flutter installations while also benefiting from the extension (is there a way to configure this extension or my environment as a whole)?
Are my expectations to support multiple flutter installations unreasonable?
If my expectations are reasonable, and there are no known or documented way to support multiple flutter installations while also using this extension, then perhaps the extension has a bug?

Package is not showing up in the dart packages folder even though it is installed

I have been having issues with installing this package https://pub.dev/packages/graphql_flutter/install one of it's dependencies is this graphql package https://pub.dev/packages/graphql. When i do pub get it installs en shows everything expect for the graphql package in the dart packages folders. I have tried to install the graphql package separately but the package still doesnt show up than. And the import still gives the error "Target of URI doesn't exist: 'package:graphql/client.dart'." But it does show up in flutter/.pub-cache/hosted/pub.dartlang.org so i dont understand why it doesnt show in my project. I have tried using flutter clean, flutter clean pub cache and androidstudio's invalidate caches/restart function to no avail. I have also tried restarting android studio and my pc multiple times. I have also tried it with androidstudio run as admin.
This is my pubspec.yaml
this is part of my Dart packages folder as you can see it does not show
And this is the pub.dartlang.org folder on my pc
It ended up getting randomly fixed after 3 days. I just started android studio up and tried it again and suddenly it worked. No idea what fixed it in the end
Seen this issue a few times. Open Task manager in windows and check for adb.exe. There should be one copy running. I had 2 copies so removed 1 and restarted the other.
right-click --> end task for both.
All ok then.
Reloading VS code worked for me..
Reloading can be done ---> Command + R for Mac.

Could not find a file named "pubspec.yaml" for flutter 1.12.13+hotfix.5

my application was working normally with flutter version1.9.1+hotfix.6
when I upgrade flutter to version1.12.13+hotfix.5
the error
Error generating build_script snapshot: Could not find a file named
"pubspec.yaml" in
"C:\Users\t.ghassan\AppData\Roaming\Pub\Cache\hosted\pub.dartlang.org\analyzer-0.35.4".
appears for both the stable channel and the master channel
I didn't use analyzer package in my application
what to do to fix it?
I'm using windows 10
analyzer is used for all apps to allow flutter analyze command from CMD.
What you can do is:
Close all IDE or files related to you flutter project
Open "C:\Users\t.ghassan\AppData\Roaming\Pub\Cache" folder
Delete "hosted" folder
Open your project again and run flutter packages get
If this doesn't help let me know in the comments.

Update flutter dependencies in /.pub-cache

I erased in my folder .pub-cache/hosted/pub.dartlang.org/this_plugin
What is the command to update the dependencies inside pubsec.yaml? I believe it is
flutter packages get
The folder under .pub-cache is still not up to date.
Note: there was a pubspec.lock that I deleted
Disclaimer: By running the command below, have a really fast internet
connection or be ready to lose one hour of productive hours. ( it will
redownload every package every installed on your pc, and I mean each
and all of the versions of each packages)~TSR
flutter pub cache repair
or delete /Users/xxxxxxx/development/tools/flutter/.pub-cache/hosted/pub.dartlang.org/cloud_firestore-0.8.2+3/ and run flutter packages get again.
if all the above things fail delete the cache folder or also check the version updated in the lock file (some time lock give me the problem with updating the version number)
PS:
Why flutter pub cache repair download every package version previously used?
Remove the package from dependencies in pubspec.yaml, run flutter packages get. And then add the package to dependencies again and running flutter packages get.This process has solved the problem for me in the past.
This steps worked for me
Delete the pubspec.lock file then run the command flutter pub get again.
Clearing the projects cache
You can empty the entire projects cache to reclaim extra disk space or remove problematic packages.
run flutter pub cache clean in terminal at project directory
The below steps worked for me.
Flutter installation corrupted
The Flutter install directory is in an inconsistent state and that causes all kinds of troubles.
Causes
Unclear
Ways to fix
Run the following commands in the Flutter install directory:
git clean -xfd
git stash save --keep-index
git stash drop
git pull
flutter doctor
Corrupted cache
Especially with plugin packages, it was seen several times that the package in the pub cache was corrupted.
Symptoms
Usually syntax errors at build time about code in dependencies.
Causes
Unknown.
IDEs or editors used by developers might not prevent editing plugin files and when they navigate into plugin code they might accidentally modify the code.
Ways to fix
Run flutter pub cache repair
This might take quite some time and re-downloads every package in the cache, even outdated versions that might not be used anymore by any project on disk.
Delete ~/.pub-cache/hosted and/or ~/.pub-cache/git (for Git dependencies). This requires running flutter packages to get in all active projects on your machine afterward.
Delete a specific package or package version. Look up the exact path in the cache for a specific package in the .packages file of your project. For example for firebase_auth
Reference: https://github.com/flutter/flutter/wiki/Workarounds-for-common-issues#flutter-installation-corrupted