【问题标题】:How to pass command line arguments to web debug session in VS Code如何在 VS Code 中将命令行参数传递给 Web 调试会话
【发布时间】:2021-12-16 05:01:12
【问题描述】:

在 Flutter/dart 中编程时,我需要将参数传递给 VS Code 中的调试会话。我将以下数据添加到launch.json 中,如documentation 中所述。

{
  "configurations": {
    ..
    // Any custom environment variables to set when running the app with this
    // launch config.
    "env": {
      "DEBUG_MODE": true
    }

    // Arguments to be passed to the Dart or Flutter app.
    "args": [
        "--dart-define", 
        "DEBUG_VALUE=true",
    ],  
}

并尝试读取该值:

void main(List<String> args) {
  final debugMode = String.fromEnvironment('DEBUG_MODE');
  final debugValue = String.fromEnvironment('DEBUG_VALUE');
  ...
}

但是变量是空的,args 列表也是空的。所以请告诉我我做错了什么?

【问题讨论】:

  • @pskink but variables are empty, and args list is also empty.
  • 顺便说一句,您发布的链接是关于“toolArgs”,而不是“args”:"configurations": [ { "name": "Flutter", "request": "launch", "type": "dart", "toolArgs": [ "--dart-define", "MY_VAR=MY_VALUE", "--dart-define", "MY_OTHER_VAR=MY_OTHER_VALUE" ] } ]
  • 使用final debugMode = const String.fromEnvironment('DEBUG_MODE');
  • 很遗憾,这没有帮助。

标签: flutter dart visual-studio-code


【解决方案1】:

您在读取值时确定使用const 吗?由于我不完全理解的原因,这似乎仅在值为const 时才有效:

在我的 launch.json 中有这个:

"toolArgs": [
    "--dart-define",
    "FOO=bar",
]

代码如下:

final foo1 = String.fromEnvironment('FOO');
final foo2 = const String.fromEnvironment('FOO');
print('FOO: "$foo1" "$foo2"');

输出是

flutter: FOO: "" "bar"

因此,如果在非常量上下文中调用 String.fromEnvironment,它似乎不会返回值。

编辑:实际上,在网络中,不使用 const 会导致异常。但是使用const,它仍然可以按预期工作。

【讨论】:

  • 现在可以使用了。我不明白它是怎么发生的。也许我遇到了 VS Code“缓存”的问题。是的,这很奇怪,为什么我们需要 const 修饰符。
猜你喜欢
  • 1970-01-01
  • 2016-05-20
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-11-13
  • 2013-10-11
  • 1970-01-01
相关资源
最近更新 更多