Use fvm to change flutter version in vscode, but the flutter version not work?

7.5k Views Asked by At

I added fvm to my project and set it up like this. Now I can run all the commands for example fvm use x.x.x or fvm flutter pub get. Everything works.

BUT when running the application through VSCode directly (Play button or shortcut) it always uses my latest install SDK version. Why is it not starting with the current flutter version ?

This is my settings.json:

  {
  "dart.flutterSdkPaths": [
    "/Users/usr/fvm/versions"
  ],
    // Remove .fvm files from search
    "search.exclude": {
      "**/.fvm": true
    },
    // Remove from file watching
    "files.watcherExclude": {
      "**/.fvm": true
    }
  }

What am I missing here? How do I configure fvm to work correctly with VSCode?

2

There are 2 best solutions below

1
On BEST ANSWER

I'm assuming you have the .fvm folder set up properly? ie. It contains the symlink flutter_sdk (which points to the currently selected SDK) and the fvm_config.json.

In your settings.json file, you also need to set the location of the SDK itself:

"dart.flutterSdkPath": ".fvm/flutter_sdk",

Which will point to the same symlink.

0
On

General Apporch is below

For exists project such as change version from 3.7.10 to 3.7.12

$ fvm install 3.7.12
$ cd project
$ fvm use 3.7.12 -f

with command fvm use 3.7.12 -f, the file .fvm/fvm_config.json will update and the sybolink .fvm/flutter_sdk also was updated to version 3.7.12.

{
  "flutterSdkVersion": "3.7.10",
  "flavors": {}
}

to

{
  "flutterSdkVersion": "3.7.12",
  "flavors": {}
}

.vscode/settings.json

{
  "dart.flutterSdkPath": ".fvm/flutter_sdk",
  // Remove .fvm files from search
  "search.exclude": {
    "**/.fvm": true
  },
  // Remove from file watching
  "files.watcherExclude": {
    "**/.fvm": true
  }
}

cd to you project and use fvm use 3.7.12 -f to change version.

For new project To prevent compatibility issues with new version, may be create new project is best choice.

$ cd project
$ fvm use 3.7.12 -f
$ fvm flutter create --platforms=android --org com.xxx .

the command fvm use 3.7.12 -f will create .fvm/flutter_sdk and .fvm/fvm_config.json file.

add .vscode/settings.json to your project

Please read more detail with official website fvm

The following answers are only for the above questions

You can list all installed local sdk versions first. such as:

$ fvm list
Cache Directory:  /Users/xxx/fvm/versions

3.10.0
3.7.12
  1. edit below user xxx to your's
{
    "dart.flutterSdkPaths": ["/Users/xxx/fvm/versions"],
    // Remove .fvm files from search
    "search.exclude": {
        "**/.fvm": true
    },
    // Remove from file watching
    "files.watcherExclude": {
        "**/.fvm": true
    }
}
  1. Change Current Flutter SDK open vscode command palette, the shortcuts is

Ctrl + Shift + p for windows

Cmd + Shift + p for macOS

input command Flutter: Change SDK and select the command, this will be list all you have install sdks, select the one you wanted.

this will update the file .vscode/settings.json

{
    "dart.flutterSdkPaths": ["/Users/xxx/fvm/versions"],
    "dart.flutterSdkPath": "/Users/xxx/fvm/versions/3.7.12", // this is your select new version
    // Remove .fvm files from search
    "search.exclude": {
        "**/.fvm": true
    },
    // Remove from file watching
    "files.watcherExclude": {
        "**/.fvm": true
    }
}

don't forget remove the folder .fvm, if you use this apporch.

$ rm -rf .fvm