如何从github添加一个包?(扑)

Kos*_*dov 12 github package dart flutter

我需要使用软件包的最新源代码,而最新源尚未发布。我应该写什么pubspec.yaml才能在github中得到一个包?

下面的代码不起作用。它不会下载该软件包,也无法将其导入到我的源代码中

dependencies:
  flutter:
    sdk: flutter

  carousel_pro:
    git:
      url: https://github.com/jlouage/flutter-carousel-pro.git
Run Code Online (Sandbox Code Playgroud)

Rah*_*nge 71

上面的答案是正确的,但我添加了一些例子。

因此,要使用 pub/package/lib 而不在 pub.dev 上发布:

1. 本地 - 保存在本地某个文件夹中

dependencies:
  library_name:
   path: /path/to/library_name
Run Code Online (Sandbox Code Playgroud)

2. 托管 - 在 Github、Gitlab 等上推送。

dependencies:
  library_name:
   git: https://github.com/username/library_name
Run Code Online (Sandbox Code Playgroud)

或者瞄准确切的分支

dependencies:
  library_name:
   git:
    url: https://github.com/username/library_name.git
    ref: dev    #branch name
Run Code Online (Sandbox Code Playgroud)

或者以精确提交为目标

dependencies:
  library_name:
   git:
    url: https://github.com/username/library_name.git
    ref: e234072340    #commit reference id
Run Code Online (Sandbox Code Playgroud)

其中“ library_name ”必须与该酒吧的 pubspec.yaml 中声明的“ name ”相同。


Moh*_*din 64

我将展示这个用例,您想要访问除 main/master 之外的分支中的特定文件夹:


  amplify_flutter:
    git:
      url: git://github.com/aws-amplify/amplify-flutter.git
      ref: null-safety-master
      path: packages/amplify_flutter/
Run Code Online (Sandbox Code Playgroud)


Kos*_*dov 12

例子 pubsec.yaml

dependencies:
  flutter:
    sdk: flutter

  carousel_pro:
    git:
      url: git://github.com/jlouage/flutter-carousel-pro.git
      ref: master
Run Code Online (Sandbox Code Playgroud)

导入包的文件示例

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: [],
      ),
    );
  }
}
Run Code Online (Sandbox Code Playgroud)

注意:如果您的IDE找不到软件包,请尝试重新启动它。

  • 重要提示:上面的“url: git://...”解决方案将不再起作用,因为 git 停止支持未经身份验证的“git://”协议 https://github.blog/2021-09-01-improving -git-协议-安全-github/ (10认同)
  • 私有 github 存储库怎么样?假设我正在开发自己的包,并且不想将其发布到任何地方,这也可以吗? (3认同)
  • @Mehdico您可以指定您所依赖的包的版本。如果您询问 git 包,那么您可以在这里阅读更多相关信息 https://dart.dev/tools/pub/dependency#git-packages (2认同)

无夜之*_*之星辰 10

此外,如果需要,您还可以使用标签:

private_view:
  git:
    url: git@github.com:xxx/private_view.git
    ref: 0.0.2 # tag
Run Code Online (Sandbox Code Playgroud)


Kau*_*ani 6

这是一个例子

audio_service:
    git:
      url: https://github.com/kaushikgodhani/audio_service.git
      ref: minor #branch name
      path: audio_service  #Folder Path on Github
Run Code Online (Sandbox Code Playgroud)