MacOS使用fvm管理多个flutter版本

  1. 安装独立的dart环境

官方安装方法:https://dart.dev/get-dart
或者下载包:https://dart.dev/tools/sdk/archive

brew tap dart-lang/dart
brew install dart
  • 更新dart版本:
brew upgrade dart
  • 重新安装dart
brew reinstall dart
  • 查看当前dart版本:
$ dart --version
Dart SDK version: 2.13.1 (stable) (Fri May 21 12:45:36 2021 +0200) on "macos_x64"
  • 查看当前安装的dart信息:
$ brew info dart
dart-lang/dart/dart: stable 2.13.1, HEAD
The Dart SDK
https://dart.dev
Conflicts with:
  dart-beta (because dart-beta ships the same binaries)
/usr/local/Cellar/dart/2.13.1 (508 files, 477.8MB) *
  Built from source on 2021-05-31 at 09:44:27
From: https://github.com/dart-lang/homebrew-dart/blob/HEAD/dart.rb
==> Options
--HEAD
    Install HEAD version
==> Caveats
Please note the path to the Dart SDK:
  /usr/local/opt/dart/libexec
  1. 安装fvm

命令:pub global activate fvm

$ pub global activate fvm
Package fvm is currently active at version 2.0.5.
Resolving dependencies... (3.4s)
The package fvm is already activated at newest available version.
To recompile executables, first run `global decativate fvm`.
Installed executable fvm.
Warning: Pub installs executables into $HOME/.pub-cache/bin, which is not on your path.
You can fix that by adding this to your shell's config file (.bashrc, .bash_profile, etc.):

  export PATH="$PATH":"$HOME/.pub-cache/bin"

Activated fvm 2.0.5.
  1. 配置

.bash_profile中添加:

# fvm
export PATH="$PATH":"$HOME/.pub-cache/bin"

使.bash_profile生效:

source ~/.bash_profile

重启命令行工具,执行fvm

$ fvm
Flutter Version Management: A cli to manage Flutter SDK versions.

Usage: fvm <command> [arguments]

Global options:
-h, --help       Print this usage information.
    --verbose    Print verbose output.
    --version    current version

Available commands:
  config     Set configuration for FVM
  dart       Proxies Dart Commands
  doctor     Shows information about environment, and project configuration.
  flavor     Switches between different project flavors
  flutter    Proxies Flutter Commands
  global     Sets Flutter SDK Version as a global
  install    Installs Flutter SDK Version
  list       Lists installed Flutter SDK Versions
  releases   View all Flutter SDK releases available for install.
  remove     Removes Flutter SDK Version
  spawn      Spawns a command on a Flutter version
  use        Sets Flutter SDK Version you would like to use in a project

Run "fvm help <command>" for more information about a command.
  1. fvm相关命令

fvm官方使用文档

  • 配置fvm缓存路径(可选,默认在用户目录下fvm/versions文件夹):
fvm config --cache-path <CACHE_PATH>
  • 查看当前安装的flutter版本:
$ fvm list

No SDKs have been installed yet. Flutter. SDKs installed outside of fvm 
will not be displayed.
  • 安装指定版本的flutter:
$ fvm install 2.2.1
Flutter "2.2.1" is not installed.

Installing version: 2.2.1...
Cloning into '/Users/yuanzhiying/fvm/versions/2.2.1'...
  • 删除指定版本:
$ fvm remove 2.2.0
Removing 2.2.0...
2.2.0 removed.

此时会在用户目录下自动创建fvm/versions/2.2.1文件夹,如果本地已有flutter,可将本地的flutter拷贝到versions目录下,文件夹改为对应的版本名。当前flutter版本可以在flutter根目录下的version文件里查看。

/Users/yuanzhiying/fvm/versions/1.22.6
  • 查看已安装的flutter版本:
$ fvm list
Cache Directory:  /Users/yuanzhiying/fvm/versions

2.2.0
1.22.6
  • 查看环境信息和项目配置

FVM Version: 2.0.5
___________________________________________________

FVM config found:
___________________________________________________

Project: info_valley
Directory: /Users/yuanzhiying/mobile_life/info_valley
Version: 1.22.6
Project Flavor: None selected
___________________________________________________

Version is currently cached locally.

Cache Path: /Users/yuanzhiying/fvm/versions/1.22.6
Channel: false
SDK Version: 1.22.6

IDE Links
VSCode: .fvm/flutter_sdk
Android Studio: /Users/yuanzhiying/mobile_life/info_valley/.fvm/flutter_sdk


Configured env paths:
___________________________________________________

Flutter:


Dart:
/usr/local/Cellar/dart/2.13.1/libexec/bin/dart

FVM_HOME:
not set
  1. 使用对应版本的flutter:
    切换至项目目录,执行命令:
$ cd mobile_life/info_valley
$ fvm use 1.22.6
Project now uses Flutter [1.22.6]
HandshakeException: Connection terminated during handshake

查看当前的使用版本:

$ fvm list
Cache Directory:  /Users/yuanzhiying/fvm/versions

2.2.0
1.22.6 (active)

项目目录下会生成一个隐藏文件夹.fvm

image.png

  1. 配置项目忽略文件.gitignore
.fvm/flutter_sdk
  1. 项目配置flutter路径

选择fluttersdk路径:

image.png

自动生成当前使用的flutter路径:

image.png

image.png

重启Android studio。

  1. flutter命令的使用

此时执行flutter doctor找不到flutter命令:

$ flutter doctor
zsh: command not found: flutter

所有flutter命令前加上fvm即可:

$ fvm flutter doctor
Doctor summary (to see all details, run flutter doctor -v):
[✓] Flutter (Channel stable, 1.22.6, on macOS 11.3 20E232 darwin-x64, locale zh-Hans-CN)
[✓] Android toolchain - develop for Android devices (Android SDK version 30.0.2)
[✓] Xcode - develop for iOS and macOS (Xcode 12.5)
[✓] Android Studio (version 4.2)
[!] Connected device
    ! Error: 营的iPhone is not connected. Xcode will continue when 营的iPhone is connected. (code -13)

! Doctor found issues in 1 category.
$ fvm flutter pub get
Running "flutter pub get" in info_valley...                         1.4s
  1. flutterdart命令的使用

所有dart命令前加上fvm即可:

$ fvm dart --version
fvm: running Dart from Flutter "1.22.6"

Dart SDK version: 2.10.5 (stable) (Tue Jan 19 13:05:37 2021 +0100) on "macos_x64"
  1. 便捷使用命令

.bash_profile中设置命令的别名:

# aliases 快捷使用fvm命令
alias flutter="fvm flutter"
alias dart="fvm dart"

使生效:

source ~/.bash_profile

重启命令行工具。

$ flutter pub get
Running "flutter pub get" in info_valley...                         1.0s
$ dart --version
Running using Flutter version configured in path.

Dart SDK version: 2.13.1 (stable) (Fri May 21 12:45:36 2021 +0200) on "macos_x64"
  1. 设置全局默认的flutter版本
$ fvm global 1.22.6
Flutter "1.22.6" has been set as global
However your "flutter" path current points to:

.
to use global Flutter SDK through FVM you should change it to:

/Users/yuanzhiying/fvm/default/bin

此时fvm目录下自动生成了一个default的快捷文件夹。

image.png

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 206,839评论 6 482
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 88,543评论 2 382
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 153,116评论 0 344
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 55,371评论 1 279
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 64,384评论 5 374
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 49,111评论 1 285
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 38,416评论 3 400
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 37,053评论 0 259
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 43,558评论 1 300
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 36,007评论 2 325
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 38,117评论 1 334
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 33,756评论 4 324
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 39,324评论 3 307
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 30,315评论 0 19
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 31,539评论 1 262
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 45,578评论 2 355
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 42,877评论 2 345

推荐阅读更多精彩内容