Install libraries

In Flutter, you can install libraries (or packages) using pub.dev, the official package repository for Dart and Flutter.


Steps to Install a Library in Flutter

  1. Go to the Official Source
    Visit https://pub.dev – this is the official source for Flutter libraries/packages.

  1. Search for the Library

    • Use the search bar to find the package you need.

    • Example: Searching for http for handling HTTP requests.


  1. Add the Library to pubspec.yaml
    In your Flutter project, open the pubspec.yaml file and add the library under dependencies:

    dependencies:
      flutter:
        sdk: flutter
      {exampleLibraryName}: ^version  
    

    ⚠️ Note: Always check for the latest stable version on pub.dev.


  1. Run flutter pub get
    In the terminal, navigate to your project directory and run:

    flutter pub get
    

    This downloads the package and makes it available for use.

How to Check All Installed Libraries

To view all the libraries/packages installed in your project, check the pubspec.lock file.


Commands for Package Management

Command

Usage

flutter pub get

Installs new dependencies from pubspec.yaml.

flutter pub upgrade

Upgrades all packages to the latest compatible versions.

flutter pub add <package>

Automatically adds a package to pubspec.yaml. Example: flutter pub add http.

flutter pub outdated

Lists outdated packages in your project.


Best Practices

  1. Use the latest stable version of a package from pub.dev.

  2. Always add a version constraint to avoid breaking changes.

  3. Regularly update packages using flutter pub upgrade.

Updated on