lxq.link
postscategoriestoolsabout

Dart 包引入的几种方式

Dart 包管理存放在 pubspec.yaml 文件

image.png

上面的包都从 https://pub.dev/ 加载

image.png

这个是 Dart 和 Flutter 的官方仓库


有些时候,第三方包可能有bug,也可能需要增添功能,这个时候就会涉及到对源码的修改。

修改源码后的主流程,正常来讲应该是向第三方包开发人员提PR,等到发版之后修改依赖的版本号。

但是这个合并的时间和流程都不可预期,所以需要其他引入包的方式作为补充。


除了 pub.dev 以外, Dart 还支持以下几种引入方式:

将 Git 仓库作为依赖源

dependencies:
  flutter:
    sdk: flutter

  geolocator:
    git:
      url: https://github.com/jzyds/flutter-geolocator.git
      ref: main
      path: geolocator/

这里是将 geolocator 依赖 指向 fork 的 git 仓库

  • url 填写仓库地址
  • ref 可以写分支的名称,也可以指定一个commit hash
  • path 是指向仓库下一个具体的路径,如果是根路径不需要添加

将本地文件夹作为依赖源

dependencies:
  flutter:
    sdk: flutter

  geolocator_android:
    path: /package/geolocator_android

path 填写本地路径

将服务器地址作为依赖源

也可以将源码托管到自己的服务器

dependencies:
  flutter:
    sdk: flutter

  gpx:
    hosted: https://dart-package-server.com

hosted 为服务器文件访问地址


不管用哪种方式引入,访问目录的根路径都要有第三方包的 pubspec.yaml 文件

2022-08-07