CMake error while running flutter desktop application

Solution 1:

According to this issue on the main flutter repo, Flutter prior to version 2.9 does not support Visual Studio 2022. If you want to build while targeting Windows you'll either have to install VS 2019 alongside 2022 or use this workaround:

The current workaround is : download your appropriate flutter version, edit _cmakeVisualStudioGeneratorIdentifier in https://github.com/flutter/flutter/blob/master/packages/flutter_tools/lib/src/windows/build_windows.dart#L25-L28 to your appropriate CMake Visual Studio Generator. You can get the currently available CMake Visual Studio Generators on this page : https://cmake.org/cmake/help/latest/manual/cmake-generators.7.html#id13.

By default the _cmakeVisualStudioGeneratorIdentifier comes with CMake Visual Studio 2019 Generator. If you are going to use CMake Visual Studio 2022 Generator - firstly ensure your visual studio 2022 distribution contains Cmake 3.21 or later, refer https://cmake.org/cmake/help/latest/generator/Visual%20Studio%2017%202022.html for details.

To apply these changes on Windows:

  • Edit path\to\flutter\packages\flutter_tools\lib\src\windows\build_windows.dart, and change the constant on line 28 from 'Visual Studio 16 2019' to 'Visual Studio 17 2022'
  • Delete flutter_tools.stamp and flutter_tools.snapshot in path\to\flutter\bin\cache\ (to cause flutter to regenerate its build tools with the new source code you changed above)
  • In the project path, run flutter clean to remove cached CMake files referring to the wrong Visual Studio version
  • In the project path, run flutter run -d windows to restart the build (successfully, this time)