How to add a package from GitHub in Flutter?
Solution 1
Example of pubspec.yaml
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
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
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
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.
Related videos on Youtube
Kostya Vyrodov
Updated on January 02, 2022Comments
-
Kostya Vyrodov over 2 years
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
-
Günter Zöchbauer over 5 yearsDid you run
flutter packages get
? -
Kostya Vyrodov over 5 years@GünterZöchbauer, yes. Do you know which path should be of a github package? Default import looks like this 'import 'package:flutter_redux/flutter_redux.dart';'. May be there is another way to import a package from github?
-
Günter Zöchbauer over 5 yearsThe
.packages
file listsflutter-carousel-pro
? -
Kostya Vyrodov over 5 years@GünterZöchbauer, yes. 'carousel_pro:file:///Users/kvyrodov/Flutter/.pub-cache/git/flutter-carousel-pro-e8df71fa66dbe72fe770515737506e266d03a5fd/lib/'
-
Kostya Vyrodov over 5 yearsLet us continue this discussion in chat.
-
Günter Zöchbauer over 5 yearsTry restarting the IDE
-
Suragch over 3 yearsDocumentation: flutter.dev/docs/development/packages-and-plugins/…
-
-
Sabrina almost 4 yearsIs there any way to use a specific release version of a library?
-
Nickr almost 4 years@Mehdico you can specify the version of a package you depend on. If you're asking about git packages, then you can read more about it here dart.dev/tools/pub/dependencies#git-packages
-
Adnan Alshami over 3 yearsWhat about private github repositories. Let's say I'm developing my own package and I don't want to publish it anywhere, will that work as well?
-
Kostya Vyrodov over 3 years@AdnanAlshami, I am not sure about this case. But I think if you have an ssh key for the repository on your pc, then it should work. Does it work?
-
iamnabink about 3 yearsuse main instead of master, if you have named your repo master
-
Ulaş Kasım over 2 yearsYour answer worked for me. I am just curious about what if we want to add multiple specific folder using this 'path:' parameter. How can we achieve that?
-
Mohammed Noureldin over 2 years@UlaşKasım I have not tried that, so I cannot tell for sure.
-
Alex V over 2 yearsIMPORTANT: the solution above with "url: git://..." won't work anymore because git stopped to support unauthenticated "git://" protocol github.blog/2021-09-01-improving-git-protocol-security-github
-
BHinkson about 2 yearsSame here, I had git:// before and was getting errors saying it couldn't find the repository. When I changed it to https:// it worked fine. I did have to grant VS code access to the repo then it worked.