157

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
Kostya Vyrodov
  • 6,257
  • 5
  • 23
  • 34

8 Answers8

229

Example of pubspec.yaml


Dependency with the specific branch:

dependencies:
  flutter:
    sdk: flutter

  carousel_pro:
    git:
      url: https://github.com/jlouage/flutter-carousel-pro.git
      ref: main # branch name

Dependency with the specific commit:

dependencies:
  flutter:
    sdk: flutter

  carousel_pro:
    git:
      url: https://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.

Kostya Vyrodov
  • 6,257
  • 5
  • 23
  • 34
  • Is there any way to use a specific release version of a library? – Sabrina Jul 12 '20 at 13:56
  • 2
    @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 https://dart.dev/tools/pub/dependencies#git-packages – Nickr Jul 12 '20 at 16:46
  • 2
    What 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? – Adnan Alshami Jan 05 '21 at 05:37
  • @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? – Kostya Vyrodov Jan 06 '21 at 11:12
  • 1
    use main instead of master, if you have named your repo master – iamnabink Mar 07 '21 at 18:19
  • 10
    IMPORTANT: the solution above with "url: git://..." won't work anymore because git stopped to support unauthenticated "git://" protocol https://github.blog/2021-09-01-improving-git-protocol-security-github/ – Alex Verbitski Jan 13 '22 at 12:58
  • Can I use tag rather than ref? – 无夜之星辰 Jan 16 '23 at 10:50
66

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.

Rahul Dange
  • 2,371
  • 20
  • 15
54

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/
Mohammed Noureldin
  • 14,913
  • 17
  • 70
  • 99
  • Your 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? – Ulaş Kasım Dec 15 '21 at 11:32
  • @UlaşKasım I have not tried that, so I cannot tell for sure. – Mohammed Noureldin Dec 15 '21 at 12:20
  • I *think* you would list this entire dependency multiple times. So if you had another package at packages/my_other_dependency, you would copy and paste all of this a second time but change the dependency name (`amplify_flutter:`) to `my_other_dependency:` and then update `path:` to point at `packages/my_other_dependency/` – Jeff Neet Feb 25 '23 at 05:01
6

What's more, you can also use tag if you need:

private_view:
  git:
    url: git@github.com:xxx/private_view.git
    ref: 0.0.2 # tag
无夜之星辰
  • 5,426
  • 4
  • 25
  • 48
4

Here is one Example

audio_service:
    git:
      url: https://github.com/kaushikgodhani/audio_service.git
      ref: minor #branch name
      path: audio_service  #Folder Path on Github
Kaushik Godhani
  • 181
  • 1
  • 2
  • 6
3

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.

Code on the Rocks
  • 11,488
  • 3
  • 53
  • 61
  • 1
    Same 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. – BHinkson Feb 09 '22 at 17:58
2

To add to the list of alternates:

As of dart 2.15, authenticated private repositories are now supported.

You can publish a package to a private repository using the standard dart CLI tools.

Dart allows you to add and manage authentication tokens to via dart pub token add and dart pub token list.

Some private repositories require that you use third party tools to do the publishing.

With a repo like OnePub (https://onepub.dev) you publish as follows:

dart pub global activate onepub
onepub login // onepub adds the onepub token to the dart pub token list
cd my_package
onepub pub private // onepub adds publish_to to your pubspec.yaml
dart pub publish

You could have manually added the publish_to key to your pubspec.yaml which is of the form:

name: my_package
publish_to: https://onepub.dev/api/jbbxpsdavu/

Obviously the url will be dependant on the private repo you are using.

To use the published package in another project use:

cd <my app>
onepub pub add my_package

This results in onepub adding a private dependency of the form: (which you could have done manually)

dependencies:
  my_package:
    hosted:
      url: https://onepub.dev/api/jbbxpsdavu/
      name: my_package
    version: ^0.3.2

A big advantage of this method over a git reference is that running:

dart pub upgrade

will upgrade you to the latest version of the my_package where as with a git tag dart pub upgrade will do nothing.

This method also makes it easier for your team to add private dependencies as they only need to know the package name (i.e. they don't need to know the url nor the git tag/ref to add a dependency).

Here is a more detailed explanation of the process. It is specific to OnePub but the broad principles apply to any private repo and the exceptions have been noted above.

https://onepub.dev/show/86646173-a968-4281-af39-3c37d349bcdc

Disclaimer: I'm associated with OnePub.

Brett Sutton
  • 3,900
  • 2
  • 28
  • 53
0

you can use path if your package no the whole repository

geocoding:
    git:
      url: https://github.com/abdullahalamodi/flutter-geocoding.git
      path: geocoding #refers to flutter-geocoding/geocoding