Is it possible to add the latest version of a dependency to pubspec.yaml without finding it myself? - flutter

Lets say I am starting a new dart project. I have a few packages on pub.dev I know that I want to use, and I want to use the most recent version of these packages.
Right now, I open my browser and go to pub.dev and find each package, then check the Versions or Installing tab to find out what the latest version is so I can put that in my pubspec.yaml. Then I can run pub get
Using npm, you can run npm install <package> and npm will automatically fetch the latest version of the package and add it to package.json
Is there a pub command, syntax for pubspec.yaml, or something else that will make pub resolve the latest version of a package and use it in my project, without having to manually look up the latest version?

Normally in Flutter, the packages which you want to use need to be added to pubspec.yaml and run pub get. But there are also some IDE plugins like npm.
For example, Pubspec Assist is using on Visual Studio, you can add or update your dependencies without going pub.dev. But you have to know which packages do you want to use at least for a time.
https://marketplace.visualstudio.com/items?itemName=jeroen-meijer.pubspec-assist

Simply enter the following command in the terminal
flutter pub add provider
Of course, where it says provider, put the package you want to install.
The package with the latest version is automatically added to your pubspec.yaml.
Otherwise you can always do what #jamesdlin said and leave the version field empty.

Related

fvm keep requesting change flutter path

I am trying to use fvm on Windows.
Because my projects use mixed of languages, I need to change the version globally.
The problem is that it keeps asking me to change the flutter path.
At first,
So, I moved flutter path down to the bottom of fvm\default\bin.
Here is how I setup the path.
Now, if I change global flutter version again, the path is changed as the version of fvm, not the default... which is not in the path at all.
If it is just the message, I can ignore, but when I try to open up my project and try flutter --version the global version is not changed.
Can anybody guess what is the problem?
I did install with flutter pub global activate fvm.
I tried deactivate and activate again, removed reset the path in various ways, but keep getting same issue.
Another thing is I am keep getting Can't load Kernel binary: Invalid SDK hash message. I just assume this is kind of related to the first issue, but not of it.
Thanks.
I guess I solved this issue.
First of all, I had flutter installed version 1.22.6 at the beginning. and installed fvm with flutter pub global activate fvm. Then, the fvm version 1.3.7 installed. If I try to install flutter version 3.x.x, it fails with message like Internal Error: Could not infer Flutter Version 3.x.x or fvm version 1.3.7 doesn't support ~~~ something like that. So, I upgraded the actual flutter installed to the latest version, and also upgraded fvm to the latest version. Then, it never go back to 1.22.6 globally with the fvm.
So, I reinstalled the flutter version 1.22.6, complete uninstall fvm by flutter pub global deactivate fvm and also deleting fvm folder.(if you don't delete the folder and reinstall, it will install the same version of fvm you previously installed) And reinstall with flutter pub global activate fvm. Then, it will then install fvm 1.3.7 version. With this version of fvm, I couldn't install 3.0.2, but could install stable
but using different commands for this fvm 1.3.7 version.... like fvm use stable --global... you can find some commands here
it was because of the actual flutter version installed and fvm version I guess.... Please comment below if I was wrong.

How to add multiple packages to pubspec.yaml from command line?

I can add a package to my pubspec.yaml file from command line using:
dart pub add foo
But how to add multiple packages?
dart pub add foo, bar // Doesn't work
Right now (Dart 2.15.1), the Dart pub tool does not support adding multiple package dependencies in one command. You therefore need to run pub add for each package you want to add as dependency for your project.
A pull request for pub have recently being merged which adds support for adding multiple dependencies in one operation.
You can find the issue here: https://github.com/dart-lang/pub/issues/3273
And pull request here: https://github.com/dart-lang/pub/pull/3283
Since this has happen rather recent, my own personal guess is, that this change is not going to be part of Dart 2.16 but rather Dart 2.17, since 2.16 has been closed for further development for some time to make it ready for release in the near future.
In bash:
for pkg in redux flutter_redux redux_sagas do; flutter pub add $pkg; done;
Obv add more packages as desired, space separated.

Flutter | Error: Couldn't resolve the package

I created a package that is a collection of my own helper classes for faster developing...
The package also included firebase_analytics: which requires a native implementation for each platform i want it to run. Even if i dont wanted to use firebase at all, but still wanted to use my package i was forced to create firebase-config files for the app, because the app crashes if the firebase_analytics package does not find a valid file for the specific platform. To avoid those things i splitted my package in two packages.
The Core-Package: All helper classes that run native dart code
Optional extension: Only the classes that need the firebase implementation
So if i want to use the package without firebase i just have to depend on the core.
If i want to use the firebase variant i could simply depend the firebase package, because the extension package itself depends on the core-package in its own pubspec.yaml file.
PROBLEM: Since i restructured my package like i described, all my apps that depend on it are not building anymore. The console says that all packages that i depend on in my two packages can not be found. This error occures both ways: With firebase & without.
ERROR: (Scroll down for the complete console output)
MY ATTEMPTS:
"flutter clean" & "flutter pub get" in every package and app folder
"flutter pub cache repair"
CORE-PACKAGE Pubspec.yaml:
FIREBASE-Extension Pubspec.yaml:
Plain Console Output:
pastebin.com/m14cbqDV
Thanks for any help!!
I am facing the same issue while i was making a build in Xcode 13.So I had upgraded my flutter version 2.8.0 to 2.10.2 and also removed the version of all firebase dependencies, then the issue was finally solved.
I have faced similar issues and resolved them by removing package versions. Issues like this often arise due to mismatched versions of packages (eg firebase_core, firebase analytics). Please try to remove versions of the firebase packages or the whole packages. (like firebase_analytics :).
Sorry, my bad! As you can see i moved my dependencys to the dev section of my pubspec.yaml. They should be placed in the dependency section.

flutter pub outdated for a specific package?

When I run
flutter pub outdated
it gives me a list of all the packages I have in pubspec.yaml which are outdated. What if I have more than 100 packages in it, I'd like to know is there any command or way (except searching in terminal) to find out the information provided by above command but only for that package, something like:
flutter pub outdated xyz
where xyz is my package.
For Android Studio and IntelliJ, use Flutter pub version checker
For Visual Studio Code, use Pubspec assist
There is this plugin for Android Studio called Flutter enhancement suite which automatically detects outdated packages and suggests updates for you inside pubspec.yaml file

How to add a package from command line?

npm (and so yarn) has a greate feature that you can add needed packages just by knowing the package name (like yarn add xxx_yyy) and it adds the latest stable release to your project. Does flutter have any equivalent hero? Or we have to search our needed package on pub.dev and find version to add to our pubspec.yaml?
Add a package as a direct dependency:
flutter pub add <package-name>
Add a package as a dev-dependency:
flutter pub add -d <package-name>
Remove a package:
flutter pub remove <package-name>
Note: You can also use dart command instead of flutter above.
You can manage packages with flutter pub command.
flutter pub add - adds packages to your project's pubspec.yaml and downloads them. So you don't have to run flutter pub get.
flutter pub add <package>
Add a package to your project's dependencies.
flutter pub add --dev <package>
Similarly adds package to dev_dependencies.
flutter pub remove <package>
Removes the package from your project's dependencies.
Documentation: https://dart.dev/tools/pub/cmd
Note: (flutter pub is the same as dart pub)
Update 2
Based on #CopsOnRoad answer, now dart has add command which is the best way for adding packages from cmd. Full documentation is here.
Update
Right now you can have exactly similar experience like npm or yarn in flutter with the help of get_cli package. One of the tools it provides will let you just write the package name and it automatically installs the latest version with mentioning the version number inside the yaml file.
From its documentation
// To install a package in your project (dependencies):
get install camera
// To install several packages from your project:
get install http path camera
// To install a package with specific version:
get install path:1.6.4
// You can also specify several packages with version numbers
// To install a dev package in your project (dependencies_dev):
get install flutter_launcher_icons --dev
Old answer
About the cli verb add, there is no any equivalent in flutter and pub yet. But about the versioning and adding packages just with their names, try to add them in pubspec.yaml file without version number. Just like this:
dependencies:
http: ^0.12.0+2
mobx:
flutter_mobx:
dio: ^2.1.13