Flutter のプロジェクトを更新するにあたり、 Flutter SDK も更新しました。以前もやってるんですが、まとめたことがなかったので、一応、その時の作業手順をメモっておきます。
更新したい SDK のダウンロード
まずは更新したいバージョンの SDK をダウンロードしてきてから、そちらを使うように設定を変更するという方法を取っています。特に大きな理由もないんですが、アップグレード後にもし問題があったときに確実に戻したいので、 flutter upgrade コマンドを使わないという感じです。
もっとも、素直に fvm (Flutter version manager) を入れてもいいんでしょうが、今のところ Flutter のバージョンを切り替えて作業を行う必要性がないので、こんな風にしています。
というわけで、現時点の最新版をダウンロードしてきます。
ダウンロード後は解凍して、 SDK を保存したいディレクトリに置いておきます。
環境変数の更新
Windows の『設定』->『システム』->『バージョン情報』を開き、『システムの詳細設定』をクリックし、環境変数(ユーザーごとの環境変数)の Path にある Flutter SDK へのパスを、ダウンロードしてきたものに修正します(公式の手順はこちらを参照してください)。
パス設定が正しいことを、コマンドラインでも確認しておきます。
C:\Users\mor>flutter --version Flutter 3.22.2 • channel stable • https://github.com/flutter/flutter.git Framework • revision 761747bfc5 (3 weeks ago) • 2024-06-05 22:15:13 +0200 Engine • revision edd8546116 Tools • Dart 3.4.3 • DevTools 2.34.3 The Flutter 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: flutter --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). C:\Users\mor>
問題ないですね。
Flutter プロジェクトの設定変更
さて、 Android Studio で既存の Flutter プロジェクトを開くと、 Flutter SDK が古いままになっています。なので、これを更新してやる必要があります。
『File』->『Settings』->『Languages & Frameworks 』->『Flutter』 を開くと、 SDK を指定する箇所があります。
これを新しいバージョンの SDK を指すように変更します。
なお、ここでは、
(SDK インストールディレクトリ)/flutter
を指定します(先ほどの Path は flutter/bin だったので間違えないように気をつけます)。
設定を確実にするため、一度、 Android Studio を終了させて、再度プロジェクトを開きます。
SDK 変更による影響のチェック
プロジェクトを開きなおしたら、 build ディレクトリをいったん削除してから、再度ビルドします(実機で実行させます)。 これで問題なく動作できれば完了だったんですが、今回は一部 Breaking Changes があったようで、エラーとなりました。
なお、 Flutter SDK 3.7.12 は Dart 2.19.6 になります。一方、 3.22.2 は Dart 3.4.3 になります。 Flutter SDK 3.7.12 の時点で null safety には対応していたので、こんなアップグレードのやり方をしています。
さて、問題があったところを地道に修正してもいいんですが、 dart fix コマンドを利用すると、ある程度自動的に修正してくれるそうです。Android Studio でも下記のように dart fix の存在を教えてくれています。
早速やってみます。最初は一気に修正するのではなく --dry-run をつけて、様子をみるのが無難そうです。
D:\work\sample_project>dart fix --dry-run 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). Computing fixes in sample_project (dry run)... 36 proposed fixes in 17 files. lib\feature\common.dart unnecessary_cast • 2 fixes (中略) To fix an individual diagnostic, run one of: dart fix --apply --code=invalid_null_aware_operator dart fix --apply --code=undefined_getter dart fix --apply --code=unnecessary_cast dart fix --apply --code=unnecessary_null_comparison dart fix --apply --code=unused_catch_clause dart fix --apply --code=unused_element dart fix --apply --code=unused_import To fix all diagnostics, run: dart fix --apply D:\work\sample_project>
指摘されたもののうち、特定のもののみを修正できるようです。
D:\work\sample_project>dart fix --apply --code=undefined_getter Computing fixes in sample_project... Applying fixes... lib\feature2\common2.dart undefined_getter • 2 fixes (略) 10 fixes made in 5 files. D:\work\sample_project>
これを使えば、少しずつ修正内容を確認しながら手直しできるので、いいですね。
パッケージのアップグレード
自分のアプリ側の修正だけで終わればよかったのですが、依存しているライブラリで問題があるものがありました。
なので、これらも新しいものに替えておきます。
で、最終的にビルドが通るようになれば、完了です。
おまけ
ちなみに、最初は Flutter SDK をダウンロードするというやり方だったのを忘れていて、いきなり flutter upgrade やってしまいました。このとき、
C:\Users\mor>flutter upgrade Your flutter checkout has local changes that would be erased by upgrading. If you want to keep these changes, it is recommended that you stash them via "git stash" or else commit the changes to a local branch. If it is okay to remove local changes, then re-run this command with "--force". C:\Users\mor>
のように警告が出てきて、 upgrade に失敗しました。似たようなことで困っている人は結構いるようで、ネットにもいろいろと落ちてました。
- Flutter upgradeでgit関連のエラーが出た場合の対処方法 #Flutter - Qiita
- flutter upgrade時の警告について。
- git - "Your flutter checkout has local changes that would be erased by upgrading." when running flutter upgrade - Stack Overflow
実際に、 SDK のディレクトリに移動して、 git status を見ると
C:\Users\mor\bin\flutter_windows_3.7.12-stable\flutter>git status On branch stable Your branch and 'origin/stable' have diverged, and have 53 and 7731 different commits each, respectively. (use "git pull" to merge the remote branch into yours) Untracked files: (use "git add <file>..." to include in what will be committed) .pub-preload-cache/ nothing added to commit but untracked files present (use "git add" to track) C:\Users\mor\bin\flutter_windows_3.7.12-stable\flutter>
.pub-preload-cache 以下が未登録になってます。なるほど、これでアップグレードできなかったようですね。
ご参考までに。
まとめ
若干手間取りましたが、無事に Flutter SDK を更新できました。まあ、Flutter 3.7.12 (Dart 2.19.6) の時に Dart 3 の null safety への対応を一通りやっておいたため、比較的簡単だったんでしょうね。
とりあえず、これで(ほぼ)最新の環境で開発を進められます。