As Flutter Developers, we often face situations where we have multiple projects that use different versions of the SDK. At this point, millions of questions arise: Should I upgrade all projects? Am I wondering if all my packages support that version? Will I break the projects? Is it essential to upgrade all my projects to the latest version?
Answering these questions depends greatly on the context, which is why I'd like to introduce you to a tool that can help you.
Flutter Version Management to the rescue!
Flutter Version Management (FVM) is a tool that allows you to have multiple Flutter SDK versions installed and use them per project. Using this tool, you’ll be able to run any project using any Flutter SDK version.
Flutter Version Management Installation process
Now we know FVM and what it can do for us, let’s see how to install it.
MacOS and Linux:
Install:
brew tap leoafarias/fvm
brew install fvm
Uninstall:
brew uninstall fvm
brew untap leoafarias/fvm
Activate:
dart pub global activate fvm
Windows:
Install:
choco install fvm
Activate:
dart pub global activate fvm
Now we have the FVM installed, you probably want to add it to the .gitignore file in order to avoid upload unnecessary files to your repo, so to do that add the following paths to your .gitignore: .fvm/flutter_sdk
.fvm/fvm_config.json
Using Flutter Version Management on my project
Once the FVM is ready to use, we need to set the Flutter SDK version that we want to use for the project. You need to move to your project /lib path using the terminal and write the command:
fvm use <flutter_sdk_version>
I really recommend, based on my and my teammates experiences, quitting your code editor or IDE after setting the Flutter SDK version just to avoid conflicts with previous versions.
And that’s all, folks! Now every Flutter command that you need to execute you’ll need to add fvm word at the beginning, e.g: fvm flutter pub get.
Flutter Version Management Useful commands
Here I share a list of useful commands:
- List of installed versions: fvm list
- Change version: fvm use <new_sdk_version>
- Install version: fvm install <new_sdk_version>
- Remove version: fvm remove <flutter_sdk_version>
- List of available releases to install: fvm releases
Configure global version for Flutter Version Management
Are you the kind of person that hate writing fvm on each Flutter command?
Don’t worry. All you need to do is configure the global version. By doing this, you can have the default Flutter version in your machine, but keep the dynamic switching. It allows you not to make any changes on how you currently use Flutter, but with faster switching and version caching.
To accomplish this, you’ll need to run the following command:
fvm global <flutter_sdk_version>
After this step, you don’t need to use fvm word more on your Flutter commands… just to change the Flutter SDK version :)
Using Flutter Version Management with flavors
You can also use different Flutter SDK versions for different project environments or release type better knows as flavors, let’s see how to configure the flavors for FVM.
Go to the .fvm/fvm_config.json path and by default you will see something like this:
Now you only need to add your flavors inside the “flavors” object like this:
More useful commands… for flavors
- To choose a specific version for a flavor: fvm use <new_sdk_version> --flavor <flavor_name>
- To switch between flavors: fvm flavor <flavor_name>
- To see the list of flavors: fvm flavor
Conclusions on Flutter Version Management
For us (Flutter developers), FVM comes in handy. In general, it is always recommended to keep your project updated if you are involved in several projects or want to use a different SDK version than the one you already have. However, there are times when you can't do it, and using FVM is the best way to avoid the questions we already asked at the beginning.