在Flutter启动配置中使用preLaunchTasks

kwvwclae  于 2023-02-25  发布在  Flutter
关注(0)|答案(1)|浏览(112)

我正在尝试运行一个preLaunchTask来定义环境变量,问题是当flutter启动在调试控制台中运行时,任务在一个单独的终端中运行。
是否有方法在调试控制台中启动任务?
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"
        }
    ],
}

我已经在dart pad中测试了实际的字符串,它工作正常。我还在调试控制台中运行了const bool.hasEnvironment,它返回了一个false。很明显,在dart启动之前,env变量没有加载到调试控制台中。我该怎么做?

xxe27gdn

xxe27gdn1#

我最初的目标是通过文件加载env变量。在Flutter 3. 7中,他们添加了--dart-define-from-file,它允许您在一个JSON文件中定义您的环境。

{
   "VAR1": "foo",
   "VAR2": "bar"
}

相关问题