How to use "–dart-define=" in Flutter test

Issue

I’m using flutter test with --dart-define option as follows:

# code to run the test
flutter test --machine --start-paused --plain-name "" --dart-define=SOMEENVVAR=123 test/rovaf_client_test.dart

but the problem is that I can’t use this environment variable as follows:

print(String.fromEnvironment("SOMEENVVAR"));

this just prints out nothing.

Is there any way to use environment variable or pass parameters for flutter run test command? Thanks!

Solution

You need a const for environment variable.

print(const String.fromEnvironment("SOMEENVVAR"));

Please refer to this:
https://github.com/flutter/flutter/issues/55870#issuecomment-620776138

Answered By – Victor Kwok

Answer Checked By – David Marino (FlutterFixes Volunteer)

Leave a Reply

Your email address will not be published. Required fields are marked *