DEV Community

Ashish Rawat
Ashish Rawat

Posted on • Edited on

Pass arguments from command line to main in Flutter/Dart?

You can add the --dart-define= in you flutter run command
Like this

flutter run --dart-define=environment="QA"

Now access the variable in the dart file

const environment =
      String.fromEnvironment('environment', defaultValue: 'Testing');

if you are using flavours you can use this command

 flutter run --flavor core_app -t lib/core_app.dart --dart-define=environment="QA"

Thanks

Top comments (2)

Collapse
 
sultanmyrza profile image
Kasymbekov Sultanmyrza

Didn't worked for me

flutter doctor

Flutter (Channel master, 1.19.0-2.0.pre, on Mac OS X 10.15.5)

--dart-define is not working returning empty value as mentioned above --dart-define got reverted github.com/flutter/flutter/pull/52041 and I also could not get it to work

Collapse
 
samoray1998 profile image
Jamal koukou

that's help but how to pass args using command line for flutter web