flutter: String.fromEnvironment is empty when --dart-define used in launch.json - flutter

I am using vscode and want to set the dart-define in launch.json for my flutter application.
In launch.json, I follow https://dartcode.org/docs/using-dart-define-in-flutter/ and have
{
"name": "development debug emulator-5554 A11 API30",
"request": "launch",
"type": "dart",
"flutterMode": "debug",
"args": [
"--flavor",
"development"
],
"toolArgs": [
"--dart-define",
"environment=development",
"--dart-define",
"STS_TOKEN_CLIENT_ID=bbbbb",
"--dart-define",
"STS_TOKEN_CLIENT_SECRET=ccccc"
],
"env": {
"MARKETING_VERSION": "1.0.0"
},
"deviceId": "emulator-5554"
},
And in my code, I am using
const clientId = String.fromEnvironment('STS_TOKEN_CLIENT_ID');
const clientSecret = String.fromEnvironment('STS_TOKEN_CLIENT_SECRET');
to retrieve values. However, both clientId and clientSecret are empty string.
Can anyone please help on it? Thank you

Related

Use preLaunchTasks in flutter launch config

I am trying to run a preLaunchTask to define environment vars. The problem is that the tasks runs in a separate terminal while the flutter launch runs in the Debug Console.
Is there a way to launch the tasks in the debug console?
tasks.json
{
// See https://go.microsoft.com/fwlink/?LinkId=733558
// for the documentation about the tasks.json format
"version": "2.0.0",
"tasks": [
{
"label": "LoadEnvVars",
"command": "pwsh.exe",
"args": [
"-ExecutionPolicy",
"Bypass",
"-File",
"${workspaceFolder}/load_env_vars.ps1"
],
},
]
}
launch.json
{
// Use IntelliSense to learn about possible attributes.
// Hover to view descriptions of existing attributes.
// For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387
"version": "0.2.0",
"configurations": [
{
"name": "makon_front (pwsh)",
"request": "launch",
"type": "dart",
"args": ["--dart-define",
"FIREBASE_WEB_CONFIG=${env:FIREBASE_CONFIG_WEB}",
"--dart-define",
"USE_FB_EMULATOR=${env:USE_FB_EMULATOR}"],
"preLaunchTask": "LoadEnvVars"
},
{
"name": "makon_front (profile mode)",
"request": "launch",
"type": "dart",
"flutterMode": "profile"
},
{
"name": "makon_front (release mode)",
"request": "launch",
"type": "dart",
"flutterMode": "release"
}
],
}
I have tested the actual string in dart pad, and it works fine. I have also ran const bool.hasEnvironment in the Debug Console, it returns a false. Clearly the env vars are not loading in the debug console before dart launch. How do I do that?

How to configure launch.json for NextJs and Typesctipt

I am trying to create a launch.json file for the following repo:
https://github.com/zakariamofaddel/shopify-nextjs-template
I have tried both the default VS Code node template and the NextJs launch file.
VS code default Node generated .vscode/launch.json file
{
"version": "0.2.0",
"configurations": [
{
"type": "node",
"request": "launch",
"name": "Launch Program",
"skipFiles": [
"<node_internals>/**"
],
"program": "${workspaceFolder}\\server\\index.ts"
}
]
}
I am able to run yarn dev successfully and use console.log:
https://github.com/zakariamofaddel/shopify-nextjs-template/blob/38c700d8706818aa12d892b3f1193a969919e003/package.json#L9
I found the solution after adding the TypeScript Debugger extension in VS code.
{
"version": "0.2.0",
"configurations": [
{
"name": "Debug Localhost",
"type": "node",
"request": "launch",
"args": [
"${workspaceRoot}\\server\\server.ts"
],
"runtimeArgs": [
"-r",
"ts-node/register/transpile-only"
],
"cwd": "${workspaceRoot}",
"protocol": "inspector",
"internalConsoleOptions": "openOnSessionStart",
"env": {
"NODE_ENV": "development"
}
}
]
}

How to run flutter in chrome with args in VS Code

How can I run my flutter app in chrome with args?
I want to run/debug/attach my flutter app in a chrome session with --disable-web-security.
In the end I would expect to have a single launch configuration that does launch my app in a single chrome session (similar to flutter run -d chrome).
In the launch.json, I did play around with attach/launch, compounds, tasks but could not make it work.
{
"version": "0.2.0",
"compounds": [
{
"name": "Launch D1",
"configurations": [
"Launch Chrome",
"app1"
],
}
],
"configurations": [
{
"name": "Launch Chrome",
"request": "launch",
"type": "chrome",
"webRoot": "${workspaceFolder}/app1",
"runtimeArgs": [
"--disable-web-security",
"--user-data-dir=%APPDATA%\\..\\Local\\tempc"
],
},
{
"name": "app1",
"cwd": "app1",
"request": "launch",
"type": "dart",
"deviceId": "chrome"
}
]
}
If you're having certificate issues running on web, you can disable the warnings using "runtimeArgs": ["--ignore-certificate-errors"]
You can also follow other workarounds mentioned on this GitHub issue thread.

Flutter: add flavors configuration into Visual Studio

My flutter application has different flavors.
In Android Studio everything is setup in the flavor configuration panel, but where can I do that in Visual Studio Code?
I guess I have to edit the configuration.json but I cant find any reference online on how to do it.
I do not want every time to type flutter run --flavor app1 -t lib/main_app1.dart
You can pass additional arguments using a launch.json. If you don't already have one, click the Cog icon in the Debug sidebar and then select Flutter in the snippet-completion list. You can then add an args section, like this:
{
"name": "Flutter",
"request": "launch",
"type": "flutter",
"args": [
"--flavor",
"app1"
]
}
You have to edit your launch.json file
{
"name": "dev", // you can add nickname for it
"request": "launch",
"type": "dart",
"args": [
"--flavor", //flavor name
"dev",
"-t", // if your have different main file
"lib/main_dev.dart"
]
},
{
"name": "App Dev",
"cwd": ".",
"request": "launch",
"type": "dart",
"flutterMode": "debug",
"program": "lib/main_dev.dart",
"args": ["--flavor", "dev"],
}

VSCode and flutter, how to connect multiple devices?

I am using Visual Studio Code on macOS for developing Flutter apps.
I can select a single device in the bottom-left of VSC. I can also run on multiple devices using flutter run -d all. I am wondering how I can run on multiple devices using the debug console in VSC. Or, at the very least, debug one device but show updates on all.
Thank you
If you're on recent versions of Flutter and the Dart/Flutter extensions (Dec 2019 onwards) this is now supported using VS Code's compound launch configurations.
Your .vscode/launch.json should contain an entry for each device, along with its deviceId (this is the ID you would pass to flutter run -d xxx):
{
"version": "0.2.0",
"configurations": [
{
"name": "Current Device",
"request": "launch",
"type": "dart"
},
{
"name": "Android",
"request": "launch",
"type": "dart",
"deviceId": "android"
},
{
"name": "iPhone",
"request": "launch",
"type": "dart",
"deviceId": "iphone"
},
],
"compounds": [
{
"name": "All Devices",
"configurations": ["Android", "iPhone"],
}
]
}
For more information, see https://github.com/flutter/flutter/wiki/Multi-device-debugging-in-VS-Code.
How about this one, it worked for me
flutter run -d all
well you can run only two devices or two virtual machine at the same time
one using command flutter run -d <put the id of the device>
and the other using f5 and choose the other device
In Flutter 1.12 support multi device debugging in VS Code
https://github.com/flutter/flutter/wiki/Multi-device-debugging-in-VS-Code
If you have different flavors, you can configure your launch.json config as follows.
{
"version": "0.2.0",
"configurations": [
// config "Run Dev Android" and "Run Dev Iphone" will be user for multiple debuging,
{
"name": "Run Dev Android",
"request": "launch",
"deviceId": "SM",
"type": "dart",
"program": "lib/app/flavors/main_development.dart",
"flutterMode": "debug",
"args": [
"--flavor", "development",
]
},
{
"name": "Run Dev Iphone",
"flutterMode": "debug",
"deviceId": "Iphone",
"program": "lib/app/flavors/main_development.dart",
"type": "dart",
"args": [
"--flavor", "development",
]
},
{
"name": "Run Dev",
"program": "lib/app/flavors/main_development.dart",
"flutterMode": "debug",
"deviceId": "Android",
"type": "dart",
"args": [
"--flavor", "development",
]
},
{
"name": "Run Stage",
"program": "lib/app/flavors/main_staging.dart",
"flutterMode": "debug",
"type": "dart",
"args": [
"--flavor", "staging"
]
},
{
"name": "Run Prod",
"program": "lib/app/flavors/main_development.dart",
"flutterMode": "release",
"type": "dart",
"args": [
"--flavor", "production"
]
},
],
"compounds": [{
"name": "All Devices",
"configurations": ["Run Dev Android", "Run Dev Iphone"],
}]
}
Now you can select the All Device and hit run.