I have a stable flutter channel SDK located at c:\flutter. which is set at the system environment variables to be the default path for Flutter.
And I'm using this path c:\flutter when creating new Flutter project in IntelliJ for our customers.
I also downloaded Flutter master channel at c:\flutter_master and I need to use this flutter SDK (master) for another project.
How I can correctly have two working flutter version on the same device for different projects without playing with the system environment variables each time?
Each version can have different versions. For example, the stable channel uses the latest stable version, while the beta channel uses a pre-release version. To change the channel, you can use flutter channel {channelName} command.
Flutter SDK can be specified per workspace if you use VSCode. You need to:
mkdir ~/flutter_dev
cd ~/flutter_dev
git clone https://github.com/flutter/flutter.git .
.vscode/settings.json
with the following content:{
"dart.flutterSdkPath": "/Users/youruser/flutter_dev"
}
See more info in Dart Code - Quickly Switching Between SDK Versions
Firstly you need to download all the flutter sdks you would want to be able to switch locally and create aliases and this allows you to use multiple versions of the sdk through the command line or the terminal, Just like you use any flutter command, And Incase you want to use these different versions of your SDK in your IDE, you need to add the SDK paths to the settings of your IDE. Below you can find the steps to add the path to vscode. The below answer will help you setup the different versions of SDK regardless of whether you are on Windows, Linux, or mac.
This is how I have done it on an M1 mac,
I have different versions of flutter SDKs installed in a Documents
folder located at $HOME/Documents
In order to access the appropriate version of flutter through the terminal, we need to create aliases. Think of aliases as a shortcut to accessing the SDK via the command line.
.bash_aliases
file inside your $HOME directoryyou can do this via terminal by running
nano ~/.bash_aliases
Paste these aliases with the appropriate path in the file.
alias flutterd='~/Documents/flutter_dev/bin/flutter'
alias flutterm='~/Documents/flutter_master/bin/flutter'
alias flutterb='~/Documents/flutter_beta/bin/flutter'
Note that you can name the aliases as you like.
I have used the name
flutterd
to point to flutter_devflutterm
to point to flutter_masterflutterb
to point to flutter_betathat means when you type flutterd
in the terminal then it will use the SDK located at ~/Documents/flutter_dev/bin/flutter
and respectively for rest of the aliases.
(Hit ctrl + x and enter to save and exit)
if [ -f ~/.bash_aliases ]; then
. ~/.bash_aliases
fi
which is basically the rc file
$HOME/.bashrc
if you are using bash
$HOME/.zshrc
file if you are using zsh
if you are not sure then typing
echo $SHELL
in your Terminal tells you which shell you’re using. This is the same file where you have added your flutter sdk's path when you first installed it. And if the file doesn't exist you may create it.
source $HOME/.<rc file>
to refresh the current terminal window.Now you can verify by typing your alias names in the terminal flutterm, flutterd
etc and it will respond from the respective sdk.
you can verify this by running <alias name> doctor -v
e.g to verify flutterd is pointing to dev run flutterd doctor -v
Here is my output when I run the command
On windows, I have the flutter SDKs stored in C:/flutter_sdk
and then create an Alias folder and create batch files corresponding to each flutter SDK, where each batch file contains the path to flutter SDK
e.g flutterd.bat
contains the path to dev
sdk
@echo off
C:\flutter_sdk\dev\bin\flutter %*
Name your batch files wisely, because you will be using them from the command line. e.g I have a batch file named as
flutterb.bat
to point to the beta channel, so to access the beta SDK I will useflutterb
in the command line and notflutter
.
and finally, we need to add the alias folder to the environment variable in order to make it accessible throughout windows.
Go to environment variables => user variables => Path => edit=> new
Now you can verify if everything works fine by opening command prompt and enter flutterb doctor
and it should show the SDK pointing to beta
Now to access the appropriate version of the SDK in vscode you need to add these SDK paths in settings.
sdk path
Now when you open a flutter project you can choose your desired version by clicking on the flutter version at the bottom
And it will prompt you to choose the sdk to use
Note that if you are changing versions from vscode, you should also run flutter pub get
from the right top icon in pubspec.yaml
, so that the source code updates as per the choosen sdk. You may confirm this by looking at the class definition of the source code.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With