Flutter + VsCode:--dart-define 未(始终)应用

问题描述 投票:0回答:1

创建一个新的演示“计数器”应用程序(Flutter 演示主页),并将

MyApp
更改为:

// ...
  // This widget is the root of your application.
  @override
  Widget build(BuildContext context) {
    print(const String.fromEnvironment('TESTVAR1'));
    print(const String.fromEnvironment('TESTVAR2'));
    return MaterialApp(
// ...

然后生成一个

.vscode/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": "example_app",
            "request": "launch",
            "type": "dart",
            "toolArgs": [
                "--dart-define=\"TESTVAR1=value1\"",
                "--dart-define",
                "TESTVAR2=value2"
            ],
            "args": [
                "--dart-define=\"TESTVAR1=value1a\"",
                "--dart-define",
                "TESTVAR2=value2a"
            ]
        },
        {
            "name": "example_app (profile mode)",
            "request": "launch",
            "type": "dart",
            "flutterMode": "profile",
            "toolArgs": [
                "--dart-define=\"TESTVAR1=value3\"",
                "--dart-define",
                "TESTVAR2=value4"
            ],
            "args": [
                "--dart-define=\"TESTVAR1=value3a\"",
                "--dart-define",
                "TESTVAR2=value4a"
            ]
        },
        {
            "name": "example_app (release mode)",
            "request": "launch",
            "type": "dart",
            "flutterMode": "release",
            "toolArgs": [
                "--dart-define=\"TESTVAR1=value5\"",
                "--dart-define",
                "TESTVAR2=value6"
            ],
            "args": [
                "--dart-define=\"TESTVAR1=value5a\"",
                "--dart-define",
                "TESTVAR2=value6a"
            ]
        }
    ]
}

现在在 Android 模拟器和物理 Android 设备上运行此应用程序。

对我来说,Android 设备始终打印:

(2) I/flutter (14855): 

(即 2 次空字符串)

Android 模拟器在上面的打印(2x 空字符串)和打印以下内容之间变化:

I/flutter ( 4461): 
I/flutter ( 4461): value2a

我希望努力在所有环境(模拟器、调试设备、设备上的独立 apk)上一致地使用我的应用程序中的环境变量。此时,它有时有效,但大多数情况下无效。这可能是什么原因造成的,如何解决?


在 Windows 11 上运行

flutter --version

Flutter 3.22.2 • channel stable • https://github.com/flutter/flutter.git
Framework • revision 761747bfc5 (4 months ago) • 2024-06-05 22:15:13 +0200
Engine • revision edd8546116
Tools • Dart 3.4.3 • DevTools 2.34.3
flutter doctor
Doctor summary (to see all details, run flutter doctor -v):
[√] Flutter (Channel stable, 3.22.2, on Microsoft Windows [Version 10.0.22631.4169], locale nl-NL)
[√] Windows Version (Installed version of Windows is version 10 or higher)
[√] Android toolchain - develop for Android devices (Android SDK version 34.0.0)
[√] Chrome - develop for the web
[!] Visual Studio - develop Windows apps (Visual Studio Community 2022 17.8.3)
    X Visual Studio is missing necessary components. Please re-run the Visual Studio installer for the "Desktop development with C++" workload, and include these components:
        MSVC v142 - VS 2019 C++ x64/x86 build tools
         - If there are multiple build tool versions available, install the latest
        C++ CMake tools for Windows
        Windows 10 SDK
[√] Android Studio (version 2024.1)
[√] VS Code (version 1.93.1)
[√] Connected device (6 available)
[√] Network resources

! Doctor found issues in 1 category.
android flutter dart visual-studio-code dart-define
1个回答
0
投票

我相信您的 dart-define 定义中存在格式问题。此外,您只需要使用

args
字段,而不是
toolArgs
。在
args
定义中,定义格式为:

"--dart-define=\"TESTVAR1=value1a\"",
"--dart-define","TESTVAR2=value2a"

根据您的信息,您说

value2a
始终在打印。因此,您应该更改
value1a
的定义,如下所示:

"--dart-define","TESTVAR1=value1a",
"--dart-define","TESTVAR2=value2a"

我还建议查看 Andrea 的这篇文章,了解 如何将 --dart-define 与 .env 文件或 json 文件一起使用。他是一位 Google Flutter 开发专家,经常发布有关构建 Flutter 的文章。

还有另一个很受欢迎的包,称为 envied,它可以解析 .env 文件并通过 build_runner 注入值。

© www.soinside.com 2019 - 2024. All rights reserved.