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
Related
I want to build a package for Flutter and Dart, but I don't have any previous experience with package development. I am not sure how to create the package. Should I use
flutter create --template=package
or
dart create -t package
I want support for both Flutter and Dart.
I looked at the Flutter documentation on creating packages, but I couldn't understand if it would create a package with Flutter only support, or both Flutter and Dart support.
First, use the following command to check if your package meets all the requirements to publish the package.
dart pub publish --dry-run
Once you are sure that your package is ready to deploy on pub dev run this command:
dart pub publish
Running "flutter pub get" in mobile...
Because mobile depends on charts_flutter from path which doesn't exist (could not find package charts_flutter at "../mobile-dependencies/pub.dartlang.org/charts_flutter-0.12.0"), version solving failed.
pub get failed (66; Because mobile depends on charts_flutter from path which doesn't exist (could not find package charts_flutter at "../mobile-dependencies/pub.dartlang.org/charts_flutter-0.12.0"), version solving failed.)
Process finished with exit code 66
/Users/H025712/development/flutter/bin/flutter --no-color pub get
Running "flutter pub get" in mobile...
Because mobile depends on charts_flutter from path which doesn't exist (could not find package charts_flutter at "../mobile-dependencies/pub.dartlang.org/charts_flutter-0.12.0"), version solving failed.
pub get failed (66; Because mobile depends on charts_flutter from path which doesn't exist (could not find package charts_flutter at "../mobile-dependencies/pub.dartlang.org/charts_flutter-0.12.0"), version solving failed.)
Cause: Someone in your team (could be yourself) add a plugin from their local folder. This plugin can't be found when you try to update the dependency.
Quick fix: If you use remote git, let you teammate include that plugin in the project folder, modify the pubspec.yaml path, add it to stage, commit then push. In the case not using git remote, copy those files to your computer in the path as pub spec.yaml suggested.
Ideal fix: Let your teammate use public plugin or share that plugin in private with password, then let flutter download that plugin by flutter pub get/upgrade.
Check your pubspec.yaml is well ordered, I had badly composed dev_dependencies and it failed me
Talked to my tech lead, who is familiar with the code; he said to check out the missing code that was referencing overrides in the pubspec.yaml:
https://dart.dev/tools/pub/dependencies
Dependency overrides
You can use dependency_overrides to temporarily override all references to a dependency.
For example, perhaps you are updating a local copy of transmogrify, a
published library package. Transmogrify is used by other packages in
your dependency graph, but you don’t want to clone each package
locally and change each pubspec to test your local copy of
transmogrify.
In this situation, you can override the dependency using
dependency_overrides to specify the directory holding the local copy
of the package.
The pubspec would look something like the following:
name: my_app
dependencies:
transmogrify: ^1.2.0
dependency_overrides:
transmogrify:
path: ../transmogrify_patch/
i can not use either install the package
Update the dependencies in pubspec.yaml
use flutter pub upgrade
also, attach the description of the place where it is used.
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.
A few months ago I made an android application using flutter. The application requires various dependencies such as intl, cached network image, stopper, carousel_slider, etc.
Then right now I want to convert the project to flutter_web. Here are the steps that I did:
clone github web flutter
I run the command "flutter packages pub global activate webdev"
Then I edited pubspec.yaml and I adjusted it to pubspec.yaml in the previous project (android project)
Here is the contents of pubspec.yaml on the web flutter project:
name: flutter_web.examples.hello_world
environment:
# You must be using Flutter> = 1.5.0 or Dart> = 2.3.0
sdk: '> = 2.3.0-dev.0.1 <3.0.0'
dependencies:
flutter_web: any
flutter_web_ui: any
stopper: ^ 1.0.1
dev_dependencies:
build_runner: ^ 1.4.0
build_web_compilers: ^ 2.0.0
dependency_overrides:
flutter_web:
git:
url: https://github.com/flutter/flutter_web
path: packages / flutter_web
flutter_web_ui:
git:
url: https://github.com/flutter/flutter_web
path: packages / flutter_web_ui
provider:
git:
url: https://github.com/kevmoo/provider
ref: flutter_web
When I run 'pub dev', an error occurs while resolving the package stopper. Then I tried to open github from the package stopper (https://github.com/aryzhov/flutter-stopper), after that I checked the files in the lib folder. There I found a file called stopper.dart. It turns out that in the file, still using
import 'package: flutter / material.dart';
So that makes my pubspec error. Because the code should be replaced with
import 'package: flutter_web / material.dart';
Therefore, I tried to outsmart the problem by removing the dependency stopper on pubspec.yaml, then I created a stopper.dart file manually, then I saved it in the lib/mypackages folder. After that, I import stopper.dart from the lib/mypackages folder (it's no longer through the package).
The package stopper is just one of the packages that I use on my Android project. Actually there are many other packages that have the same problem as the package stopper. The point is I have to create the package files manually, then I change "package: flutter / ..." to "package: flutter_web /" manually, then I can import them.
What I want to ask, is there a more elegant and simple way?
Hi the instruction you are following is from an old repository here. As you can see this is discontinued. You can find the informaiton in the REadme.
Instead you should follow the instructions in this page. Read it once carefully and I would suggest to keep a backup of your main project and work on a copy.
First you will have to enable the flutter-web using flutter config --enable-web.
Then you will have to run flutter create .