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  [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 " for more information about a command.
  1. fvm相关命令

fvm官方使用文档

  • 配置fvm缓存路径(可选,默认在用户目录下fvm/versions文件夹):
fvm config --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

你可能感兴趣的:(MacOS使用fvm管理多个flutter版本)