30

npm (and so yarn) has a great 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?

4
  • No Flutter doesn't support npm or yarn command Flutter only allows to add the package by adding into pubspec.yaml
    – Rishabh
    Jul 26, 2019 at 4:55
  • @axelblaze, I myself have already know it does not support npm or yarn, I'm asking about a same THING. Jul 26, 2019 at 5:50
  • I don't think any such tool exist yet, but I think it is possible to create one though.
    – 10101010
    Jul 26, 2019 at 7:07
  • @10101010 Agree. But I don't really have any problem with current situation to be honest. Jul 26, 2019 at 7:15

3 Answers 3

61
  • 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.

2
  • How to add packege to dev_dependencies from command line?
    – e79ene
    May 2, 2021 at 4:21
  • 1
    @e79ene Use d for example: dart|flutter pub add -d xxx
    – CopsOnRoad
    May 2, 2021 at 11:30
7

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)

1
  • How can we add multiple packages with flutter pub add ?
    – user11874694
    Sep 29, 2021 at 23:23
5

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
2
  • 1
    Thanks! I wish flutter would add the version (after fetching the package for the first time), so it doesn't fetch it again (to get the newer version). Jul 26, 2019 at 16:45
  • @AlirezaXX pubspec.lock file will do. due to the dart documentation you have to add it to source control (just for non-shared applications) Jul 27, 2019 at 7:55

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.