开发Dart packages

2020-04-01  本文已影响0人  tom__zhu

step 1 创建package

首先需要使用命令行创建package flutter create --template=package PACKAGENAME
这行命令会创建一个名为PACKAGENAME的package项目,其中包含如下文件。

step 2 编写执行代码

在lib/PACKAGENAME.dart文件编写实现代码,或者在 lib 目录下添加具体实现文件然后再import到lib/PACKAGENAME.dart文件中。需要注意,package实现代码需要在 lib 目录内,如想进一步了解package内容管理可以参考 dart library package
test 目录下可以增加Unit tests

step 3 准备上传pub

  1. 添加License并修改pubspec.yaml 参考 选择软件协议 常用的是MIT

  2. 完善pubspec.yaml中name description version homepage author

  3. 执行 flutter pub pub publish --dry-run 对.yaml配置做检查,按照Suggestions提示做修改知道没有warning。

  4. 执行 flutter pub publish 发布。

在发布package之前需要确认 ~/.bash_profile 中是否有 PUB_HOSTED_URLFLUTTER_STORAGE_BASE_URL 这两个变量,如果存在是需要被注释掉的,不然会受到如下的错误提示:

Waiting for your authorization...
Authorization received, processing...
It looks like accounts.google.com is having some trouble.
Pub will wait for a while before trying to connect again.
OS Error: Operation timed out, errno = 60, address = accounts.google.com, port = 56264
pub finished with exit code 69

上传需要CLI翻墙,不然会收到如下提示:

It looks like accounts.google.com is having some trouble.
Pub will wait for a while before trying to connect again.
OS Error: Operation timed out, errno = 60, address = accounts.google.com, port = 58993
pub finished with exit code 69

Developing Dart packages

上一篇下一篇

猜你喜欢

热点阅读