Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 917
Alireza Akbari Avatar asked Jul 26 '19 04:07

Alireza Akbari


3 Answers

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

like image 168
CopsOnRoad Avatar answered Oct 07 '22 10:10

CopsOnRoad


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)

like image 22
e79ene Avatar answered Oct 07 '22 11:10

e79ene


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
like image 4
Hamed Hamedi Avatar answered Oct 07 '22 12:10

Hamed Hamedi