はじめに
前回はFlutter SDKのバージョンを git checkout で切り替える方法を書きましたが、
今回はFVM(Flutter Version Management)管理に変更し、バージョンを切り替える方法です。
Flutterのバージョンを切り替えたい
v3.19.4 から v3.29.0 に切り替えることになりました。
プロジェクトが1つだと手動でも困らないのですが、
複数だといろいろ困るので、やっぱりFVMも試すことにしました!
自分の備忘録として、載せたいと思います。
前提条件
・Mac
・Flutterの実行環境があること
・動作確認できるFlutterプロジェクトがあること
・VSCode使用
・iOS/Androidアプリ
やりたいこと
・現状のv3.19.4のプロジェクト環境は残したい
・特定のプロジェクトだけをFVM v3.29.0の環境にしたい
・どちらも実行できる環境を作りたい
確認結果まとめ
・(手動のgit checkoutでv3.19.4のプロジェクトが起動することを確認):OK
・FVM環境:v3.19.4のプロジェクトが起動することを確認:OK
・FVM環境:v3.29.0に変更、(エラー解消後、)起動することを確認:OK
・手動のgit checkout環境:別のv3.19.4のプロジェクトが起動することを確認:OK
1.手動のgit checkoutのFlutterのバージョンを確認する
$ flutter --version
Flutter 3.19.4 • channel [user-branch] • unknown source
Framework • revision 68bfaea224 (1 year, 1 month ago) • 2024-03-20 15:36:31
-0700
Engine • revision a5c24f538d
Tools • Dart 3.3.2 • DevTools 2.31.1
ここまでで以下確認OK
・(手動のgit checkoutでv3.19.4のプロジェクトが起動することを確認):OK
2.FVMをインストールする
$ dart pub global activate fvm
The Dart CLI developer tool uses Google Analytics to report usage and diagnostic
data along with package dependencies, and crash reporting to send basic crash
reports. This data is used to help improve the Dart platform, Flutter framework,
and related tools.
Telemetry is not sent on the very first run. To disable reporting of telemetry,
run this terminal command:
dart --disable-analytics
If you opt out of telemetry, an opt-out event will be sent, and then no further
information will be sent. This data is collected in accordance with the Google
Privacy Policy (https://policies.google.com/privacy).
+ args 2.7.0pendencies... (1.7s)
+ async 2.11.0 (2.13.0 available)
+ boolean_selector 2.1.2
+ characters 1.3.0 (1.4.0 available)
+ cli_completion 0.5.0 (0.5.1 available)
+ clock 1.1.1 (1.1.2 available)
+ collection 1.18.0 (1.19.1 available)
+ dart_console 1.2.0 (4.1.1 available)
+ dart_mappable 4.3.1 (4.5.0 available)
+ date_format 2.0.9
+ equatable 2.0.7
+ ffi 2.1.3 (2.1.4 available)
+ file 7.0.1
+ fvm 3.2.1
+ git 2.2.1 (2.3.1 available)
+ http 1.2.2 (1.3.0 available)
+ http_parser 4.0.2 (4.1.2 available)
+ interact 2.2.0
+ intl 0.18.1 (0.20.2 available)
+ io 1.0.4 (1.0.5 available)
+ json_annotation 4.9.0
+ jsonc 0.0.3
+ mason_logger 0.2.16 (0.3.3 available)
+ matcher 0.12.16+1 (0.12.17 available)
+ meta 1.16.0
+ path 1.9.0 (1.9.1 available)
+ platform 3.1.6
+ process 5.0.3
+ pub_semver 2.1.4 (2.2.0 available)
+ pub_updater 0.4.0 (0.5.0 available)
+ pubspec 2.3.0
+ quiver 3.2.2
+ scope 5.1.0
+ source_span 1.10.1
+ stack_trace 1.11.1 (1.12.1 available)
+ stream_channel 2.1.4
+ string_scanner 1.4.1
+ term_glyph 1.2.2
+ test_api 0.7.2 (0.7.4 available)
+ tint 2.0.1
+ type_plus 2.1.1
+ typed_data 1.3.2 (1.4.0 available)
+ uri 1.0.0
+ web 0.5.1 (1.1.1 available)
+ win32 5.5.0 (5.12.0 available)
+ yaml 3.1.2 (3.1.3 available)
Building package executables...
Built fvm:compile.
Built fvm:main.
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 3.2.1.
3.bashにPATHを設定する
$ cd
$ vi .bash_profile
---
# Add fvm 2025/04/xx
export PATH=$HOME/.pub-cache/bin:$PATH
---
$ source ~/.bash_profile
4.fvmのPATHが通ったことを確認する
$ fvm --version
3.2.1
5.プロジェクトでfvmのバージョンを指定する
$ cd /Users/x.user/Documents/work/Project_A
$ fvm use 3.19.4
Completion files installed. To enable completion, run the following command in your shell:
source /Users/x.user/.bash_profile
Creating local mirror...
Compressing objects: [██████████████████████████████████████████████████] 100%
Receiving objects: [██████████████████████████████████████████████████] 100%
Resolving deltas: [██████████████████████████████████████████████████] 100%
✓ Clone complete
Flutter SDK: SDK Version : 3.19.4 is not installed.
✔ Would you like to install it now? · yes
✓ Flutter SDK: SDK Version : 3.19.4 installed! (43.8s)
Setting up Flutter SDK: 3.19.4
Downloading Darwin x64 Dart SDK from Flutter engine a5c24f538d05aaf66f7972fb23959d8cafb9f95a...
% Total % Received % Xferd Average Speed Time Time Time Current
Dload Upload Total Spent Left Speed
100 214M 100 214M 0 0 31.8M 0 0:00:06 0:00:06 --:--:-- 38.4M
Building flutter tool...
Resolving dependencies... (2.5s)
Got dependencies.
Flutter 3.19.4 • channel stable • https://github.com/flutter/flutter.git
Framework • revision 68bfaea224 (1 year, 1 month ago) • 2024-03-20 15:36:31 -0700
Engine • revision a5c24f538d
Tools • Dart 3.3.2 • DevTools 2.31.1
✓ Flutter SDK: SDK Version : 3.19.4 is setup
You should add the fvm version directory ".fvm/" to .gitignore.
✔ Would you like to do that now? · yes
✓ Added .fvm/ to .gitignore
✓ Dependencies resolved. (21.8s)
✓ Project now uses Flutter SDK : SDK Version : 3.19.4
6.プロジェクトでバージョンを確認する
$ fvm --version
3.2.1
$ fvm flutter --version
Flutter 3.19.4
ここまでで以下確認OK
・FVM環境:v3.19.4のプロジェクトが起動することを確認:OK
7.プロジェクトでfvmのflutterのバージョンを変更する
$ fvm use 3.29.0
Flutter 3.29.0 • channel stable • https://github.com/flutter/flutter.git
Framework • revision 35c388afb5 (10 weeks ago) • 2025-02-10 12:48:41 -0800
Engine • revision f73bfc4522
Tools • Dart 3.7.0 • DevTools 2.42.2
8.バージョンの変更によるエラーを解消する
基本的にはエラーに従って解消していきます。
以下のコマンドも使いました。
$ fvm flutter pub upgrade --major-versions
$ fvm flutter pub upgrade win32
ここまでで以下確認OK
・FVM環境:v3.29.0に変更、試して、起動することを確認:OK
・手動のgit checkout環境:別のv3.19.4のプロジェクトが起動することを確認:OK
9.注意
FVM管理のプロジェクト内でコマンドを使いたいときは"fvm"を忘れずに!
以下似ていますが違います。
$ flutter --version
Flutter 3.19.4
$ fvm --version
3.2.1
$ fvm flutter --version
Flutter 3.29.0
VSCode上での実行は以下の設定により"fvm"付きのコマンドになります
{
"dart.flutterSdkPath": ".fvm/versions/3.29.0"
}
10.あとがき
複数のプロジェクトをFVMに移行していきたいと思います!✧٩(ˊωˋ*)و✧