How to add a package from GitHub in Flutter?
GithubDartFlutterPackageGithub Problem Overview
I need to use the latest source code of a package and the latest source hasn't been published yet.
What should I write into pubspec.yaml
to get a package in Github?
The code below doesn't work. It doesn't download the package and I can't import it into my source code
dependencies:
flutter:
sdk: flutter
carousel_pro:
git:
url: https://github.com/jlouage/flutter-carousel-pro.git
Github Solutions
Solution 1 - Github
pubspec.yaml
Example of Dependency with the specific branch:
dependencies:
flutter:
sdk: flutter
carousel_pro:
git:
url: git://github.com/jlouage/flutter-carousel-pro.git
ref: main # branch name
Dependency with the specific commit:
dependencies:
flutter:
sdk: flutter
carousel_pro:
git:
url: git://github.com/jlouage/flutter-carousel-pro.git
ref: ea12e41 # commit hash
Example of a file importing the package:
import 'package:carousel_pro/src/carousel_pro_widgets.dart';
import 'package:flutter/material.dart';
class NewsCarousel extends StatelessWidget {
@override
Widget build(BuildContext context) {
return SizedBox(
height: 200.0,
child: WidgetCarousel(
autoplay: false,
pages: [],
),
);
}
}
Note: If your IDE doesn't see the package, try to restart it.
Solution 2 - Github
The above answers are correct but I have added some examples.
So to use pub/package/lib without publishing on pub.dev :
1. Local - Save in some local folder
dependencies:
library_name:
path: /path/to/library_name
2. Hosted - Pushed on Github, Gitlab etc.
dependencies:
library_name:
git: https://github.com/username/library_name
Or to target exact branch
dependencies:
library_name:
git:
url: https://github.com/username/library_name.git
ref: dev #branch name
Or to target exact commit
dependencies:
library_name:
git:
url: https://github.com/username/library_name.git
ref: e234072340 #commit reference id
Where 'library_name' has to be the same as the 'name' declared in pubspec.yaml of that pub.
Solution 3 - Github
I will show this use case, where you want to access a specific folder in a branch other than main/master:
amplify_flutter:
git:
url: git://github.com/aws-amplify/amplify-flutter.git
ref: null-safety-master
path: packages/amplify_flutter/
Solution 4 - Github
The above didn't work for me but changing the url to use https did:
dependencies:
flutter:
sdk: flutter
flutter_tflite:
git:
url: https://github.com/qookit/flutter_tflite.git
ref: main
"main" is the name of the branch I was interested in using.
The first time I ran 'flutter pub get' it opened up a browser window to ask me for my git credentials too.