Configuring VS Code Launch Environments
To streamline the development process, you can configure Visual Studio Code to easily run your Flutter application in different environments (demo, development, or production) directly from the “Run and Debug” panel. This avoids the need to manually type the flutter run command with the correct --dart-define arguments each time.
Create a launch.json File
Section titled “Create a launch.json File”- In your project’s root directory, create a folder named
.vscodeif it doesn’t already exist. - Inside the
.vscodefolder, create a new file namedlaunch.json. - Copy and paste the following JSON configuration into your
launch.jsonfile.
{ "version": "0.2.0", "configurations": [ { "name": "Demo - Flutter News App", "request": "launch", "type": "dart", "program": "lib/main.dart", "args": [ "--dart-define=APP_ENVIRONMENT=demo" ] }, { "name": "Development - Flutter News App", "request": "launch", "type": "dart", "program": "lib/main.dart", "args": [ "--dart-define=APP_ENVIRONMENT=development", "--dart-define=BASE_URL=http://localhost:8080" ] }, { "name": "Production - Flutter News App", "request": "launch", "type": "dart", "program": "lib/main.dart", "args": [ "--dart-define=APP_ENVIRONMENT=production", "--dart-define=BASE_URL=https://api.your-production-domain.com" ] } ]}How to Use
Section titled “How to Use”After saving the file, open the “Run and Debug” panel in VS Code (you can use the shortcut Ctrl+Shift+D). You will now see a dropdown menu at the top with the launch configurations: “Demo - Flutter News App”, “Development - Flutter News App”, and “Production - Flutter News App”.
Simply select the environment you want to run and press the green play button (or F5) to launch the application with the correct settings.