在中国网络环境下使用 Flutter

欢迎你来到由中国 Flutter 社区维护的中文 Flutter 资源网站:https://flutter-io.cn

The Flutter community has made a Simplified Chinese version of the Flutter site website available at https://flutter-io.cn.

如果你需要下载 Flutter SDK 的独立打包文件,你可以将下载链接前缀替换为你信任的镜像链接。

If you’d like to install Flutter using an installation bundle, you can replace the domain of the original URL with a trusted mirror to speed it up. For example:

同时为了正常升级 Flutter 和通过 pub package 命令获取 packages,你需要设置如下两个环境变量,设置方式如下:

You must also set two environment variables to upgrade Flutter and use the pub package repository in China. Instructions are below.

为 Flutter 设定镜像配置

Configuring Flutter to use a mirror site

如果你在国内使用 Flutter,那么你可能需要找一个与官方同步的可信的镜像站点,帮助你的 Flutter 命令行工具到该镜像站点下载其所需的资源。你需要为此设置两个环境变量:“PUB_HOSTED_URL”和“FLUTTER_STORAGE_BASE_URL”,然后再运行 Flutter 命令行工具。

If you’re installing or using Flutter in China, it may be helpful to use a trustworthy local mirror site that hosts Flutter’s dependencies. To instruct the Flutter tool to use an alternate storage location, you need to set two environment variables, PUB_HOSTED_URL and FLUTTER_STORAGE_BASE_URL, before running the flutter command.

以 macOS 或者与 Linux 相近的系统为例,这里有以下步骤帮助你设定镜像。在系统终端里执行如下命令设定环境变量,并通过 GitHub 检出 Flutter SDK:

Taking MacOS or Linux as an example, here are the first few steps in the setup process for using a mirror site. Run the following in a Bash shell from the directory where you wish to store your local Flutter clone:

$ export PUB_HOSTED_URL=https://pub.flutter-io.cn
$ export FLUTTER_STORAGE_BASE_URL=https://storage.flutter-io.cn
$ git clone -b dev https://github.com/flutter/flutter.git
$ export PATH="$PWD/flutter/bin:$PATH"
$ cd ./flutter
$ flutter doctor

如上步骤设定之后,你可以继续进行 Flutter 安装的下一步:编辑工具设定,在这两个环境变量(PUB_HOSTED_URLFLUTTER_STORAGE_BASE_URL)设定过后,未来通过命令 flutter pub get 获取 packages 的时候,网络请求将会通过 flutter-io.cn 提供的镜像进行。

After these steps, you should be able to continue setting up Flutter normally. From here on, packages fetched by flutter pub get are downloaded from flutter-io.cn in any shell where PUB_HOSTED_URL and FLUTTER_STORAGE_BASE_URL are set.

flutter-io.cn 所提供的镜像由中国的 Flutter 开发者社区提供和维护, Flutter 团队无法保证其的长期稳定运作,你也可以自由使用其他可信的机构提供的镜像服务。如果你想在中国设定自己的镜像,请与通过邮件群组获得帮助:flutter-dev@googlegroups.com

The flutter-io.cn server is a provisional mirror for Flutter dependencies and packages maintained by GDG China. The Flutter team cannot guarantee long-term availability of this service. You’re free to use other mirrors if they become available. If you’re interested in setting up your own mirror in China, contact flutter-dev@googlegroups.com for assistance.

社区运行的镜像站点

Community-run mirror sites

如下列表为目前在国内提供镜像的社区,由于镜像的实现方式有所不同,可能回导致数据的滞后等问题。我们制作了一个 镜像可用性监控页面,供参考。

Flutter 社区

社区主镜像,采用多种方式同步 Flutter 开发者资源。

$ export PUB_HOSTED_URL=https://pub.flutter-io.cn
$ export FLUTTER_STORAGE_BASE_URL=https://storage.flutter-io.cn

上海交大 Linux 用户组

Pub API 返回值(archive_url)需要优化,可能造成无法访问的情况。

$ export PUB_HOSTED_URL=https://dart-pub.mirrors.sjtug.sjtu.edu.cn
$ export FLUTTER_STORAGE_BASE_URL=https://mirrors.sjtug.sjtu.edu.cn

清华大学 TUNA 协会

(推荐)定时与 Flutter 社区 Storage 镜像同步,Pub API 采取定时主动抓取策略,数据可能有延迟,已知的延迟见页面最下方。目前正在尝试进行 404 数据(延迟数据)回源(Flutter 社区镜像站)策略。

$ export PUB_HOSTED_URL=https://mirrors.tuna.tsinghua.edu.cn/dart-pub
$ export FLUTTER_STORAGE_BASE_URL=https://mirrors.tuna.tsinghua.edu.cn/flutter

CNNIC

基于 TUNA 协会的镜像服务,数据策略与 TUNA 一致,通过非教育网的域名访问。

$ export PUB_HOSTED_URL=http://mirrors.cnnic.cn/dart-pub
$ export FLUTTER_STORAGE_BASE_URL=http://mirrors.cnnic.cn/flutter

腾讯云开源镜像站

定时(每天凌晨)与 TUNA 协会镜像同步,数据有延迟,访问速度有待反馈。

$ export PUB_HOSTED_URL=https://mirrors.cloud.tencent.com/dart-pub
$ export FLUTTER_STORAGE_BASE_URL=https://mirrors.cloud.tencent.com/flutter

已知问题

  • 上海交大 Linux 用户组镜像的 Pub API 返回值(archive_url)需要优化,可能造成 Packages 无法下载的情况。

  • TUNA 协会镜像有一台负载均衡的服务器数据同步延迟(最新数据是 5/22),已经在解决。

  • 上海大学的镜像暂时只允许校内访问。