Introduction
Dart is a modern, object-oriented programming language developed by Google. Originally designed as a replacement for JavaScript in web development, Dart has evolved to become a versatile language used for mobile, desktop, and server-side applications. One of the latest features added to Dart 3 is destructuring, a powerful syntax that allows developers to extract values from collections such as lists, maps and records concisely and readably.
In the other side, Flutter is a UI toolkit for building fast, beautiful, natively compiled applications for mobile, web, and desktop that is built with Dart.
Dart & Flutter has an ecosystem of packages that can be used to build applications faster and more efficiently, from their package manager, pub.dev, where you can find a lot of packages that can be used in your projects in order to reduce devlopment time and simplifies complex tasks. However, in such cases you may need to use a package that is not available on pub.dev, and you may need to import it from a Git repository that is hosted on Github, github..
In this article, we will discover how we can use a package from a Git repository in our Dart/Flutter projects and how to import it.
What This Feature Solves And How it Can Be Beneficial For Me as a Developer
In order to answer this question let’s say we have a Dart/Flutter package called gwhyyy_dart_package
, that is hosted and developed on Github:
library gwhyyy_dart_package;
class GwhyyyDartPackage {
String helloWorld() {
return "Hello World";
}
}
This package is just for demonstration purpose and it does nothing but returning a Hello World
string.
Now, usually if this package is available on pub.dev we can simply import it in our project by adding it to the pubspec.yaml
file:
dependencies:
# if the project is a Flutter project
flutter:
sdk: flutter
gwhyyy_dart_package: any
and by running a flutter pub get
command, the package will be downloaded and imported in our project, right?
But what if the package is not available on pub.dev? and we need to import it from a Git repository, how can we do that?
This is what we will discover in the next.
How to Import A Dart/Flutter Package From A Git Repository
Well, an uncommon feature that is not known by a lot of developers is that we can import a Dart/Flutter package from a Git repository without cloning the whole repository code.., and inside the pubspec.yaml
file.
- First, we need to get the GIT url of the repository, for example, if the repository is hosted on Github, the url will be something like:
Note: this is just an example, and the package is not available on pub.dev.
- Now, we need to add the package to the
pubspec.yaml
file, and specify thegit
property with the repository url, and thepath
property with the path to the package directory inside the repository:
dependencies:
# if the project is a Flutter project
flutter:
sdk: flutter
gwhyyy_dart_package:
git: https://github.com/anasfik/gwhyyy_dart_package.git
See the difference? we didn’t set the version of the package directly as usually, but we specified the git
property with the GIT repository url, and the path
property with the path to the package directory inside the repository.
- Now, we need simply to run a
flutter pub get
command to download the package and import it in our project like you would do with a package that is available on pub.dev.
dart pub get
- Finally, we can import the package in our project and use it:
import 'package:gwhyyy_dart_package/gwhyyy_dart_package.dart';
void main() {
final gwhyyyDartPackage = GwhyyyDartPackage();
print(gwhyyyDartPackage.helloWorld()); // Hello World
}
Conclusion
In this article, we discovered how to import a Dart/Flutter package from a Git repository, and how to use it in our projects.