How to add a package from command line?

3,973

Solution 1

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

Solution 2

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

Solution 3

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)

Share:
3,973
Alireza Akbari
Author by

Alireza Akbari

Updated on December 12, 2022

Comments

  • Alireza Akbari
    Alireza Akbari over 1 year

    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 search our needed package on pub.dev and find version to add to our pubspec.yaml?