skip to Main Content

Create a new demo ‘counter’ app (Flutter Demo Home Page), and change MyApp to this:

// ...
  // 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(
// ...

Then generate a .vscode/launch.json and update it to:

{
    // 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"
            ]
        }
    ]
}

Now run this app on an Android Emulator and on a physical Android device.

For me, the Android device consistently prints:

(2) I/flutter (14855): 

(i.e., 2 times empty string)

The Android Emulator changes between printing like the above (2x empty string) and printing the following:

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

I want to work towards consistently using environment variables in my app on all environments (emulator, debugging device, standalone apk on device). At this moment, it sometimes works, but most often does not. What could cause this, and how can it be solved?


Running on 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.

2

Answers


  1. I believe you have a formatting issue in your dart-define definitions. Also, you only need to use the args field and not the toolArgs. In the args definition you have the defines formatted as:

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

    Based on your information you stated that value2a is always printing. Therefore you should change your definition of value1a to be like so:

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

    I also recommended checking out this post from Andrea about how to use –dart-define with a .env file or json file. He is a Google Developer Expert on Flutter and posts frequently about building Flutter.

    There is another package that is popular called envied which can parse a .env file and inject the values via build_runner.

    Login or Signup to reply.
  2. I could not reproduce the problem on Flutter 3.24.1 • channel stable •
    Tools • Dart 3.5.1 • DevTools 2.37.2 using the devices:

    • Linux (linux-desktop)
    • Pixel 6 (android-x64 emulator)

    The configuration I am using is the one suggested by the documentation regarding environment declarations:

    {
      "version": "0.2.0",
      "configurations": [
        {
          "name": "example_app",
          "request": "launch",
          "type": "dart",
          "toolArgs": [
            "--dart-define=TESTVAR1=value1",
            "--dart-define=TESTVAR2=value2"
          ]
        },
     ...
    }
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search