0
0
FlutterHow-ToBeginner · 3 min read

How to Install Package in Flutter: Step-by-Step Guide

To install a package in Flutter, add the package name and version to the dependencies section of your pubspec.yaml file. Then run flutter pub get in your terminal to download and link the package to your project.
📐

Syntax

In Flutter, you install packages by adding them to the dependencies section of your pubspec.yaml file. Each package entry includes the package name and its version.

After editing, run flutter pub get to fetch the package.

yaml
dependencies:
  package_name: ^version_number
💻

Example

This example shows how to add the popular http package to your Flutter project to make network requests.

yaml
name: my_flutter_app

environment:
  sdk: '>=2.17.0 <3.0.0'

dependencies:
  flutter:
    sdk: flutter
  http: ^0.14.0
Output
Package http 0.14.0 added and linked to the project.
⚠️

Common Pitfalls

Forgetting to run flutter pub get: After adding a package, you must run this command to download it.

Incorrect indentation in pubspec.yaml: YAML files are sensitive to spaces. Use two spaces for indentation.

Using incompatible package versions: Check the package's Flutter SDK compatibility before adding.

yaml
dependencies:
  http: ^0.14.0  # Wrong indentation, should be two spaces

# Correct:
dependencies:
  http: ^0.14.0
📊

Quick Reference

  • Add package: Edit pubspec.yaml under dependencies.
  • Fetch package: Run flutter pub get.
  • Import package: Use import 'package:package_name/package_name.dart'; in your Dart files.

Key Takeaways

Add packages to the dependencies section in pubspec.yaml with correct indentation.
Always run flutter pub get after editing pubspec.yaml to download packages.
Check package compatibility with your Flutter SDK version before installing.
Import the package in your Dart code to use its features.
YAML files are sensitive to spaces; use two spaces for indentation.