Skip to content
DeveloperMemos

Adding a GitHub Package to Your Flutter Project

Flutter, GitHub, Dependencies1 min read

Flutter devs often need to include packages that are hosted on GitHub(I've had to personally do this a couple of times), especially when using the latest source code of a package that hasn't been published yet. This guide explains how to add a GitHub package to your Flutter project.

1. Using a Package from a Public GitHub Repository

To include a package from a public GitHub repository, specify the package in your pubspec.yaml under dependencies.

Basic Usage

1dependencies:
2 some_package:
3 git:
4 url: https://github.com/username/some_package.git

Specifying a Branch

You can target a specific branch using the ref property.

1dependencies:
2 some_package:
3 git:
4 url: https://github.com/username/some_package.git
5 ref: branch-name

Specifying a Commit

To pin the package to a specific commit, use the commit's SHA hash in the ref property.

1dependencies:
2 some_package:
3 git:
4 url: https://github.com/username/some_package.git
5 ref: commit-hash

2. Using a Local Package

For packages that are developed locally or are not yet pushed to a remote repository, you can reference them by their local paths.

1dependencies:
2 local_package:
3 path: ../path/to/local_package

3. Importing the Package

After adding the package to pubspec.yaml, run flutter pub get in your terminal to fetch the package. Then, you can import and use the package in your Dart files.

1import 'package:some_package/some_package.dart';

Troubleshooting

  • If the IDE does not recognize the package immediately, try restarting the IDE.
  • Ensure that the package name in pubspec.yaml matches the package name defined in the package's own pubspec.yaml.