DEV Community

Ashish Rawat
Ashish Rawat

Posted on • Edited on

2 2

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

Image of Stellar post

🚀 Stellar Dev Diaries Series: Episode 1 is LIVE!

Ever wondered what it takes to build a web3 startup from scratch? In the Stellar Dev Diaries series, we follow the journey of a team of developers building on the Stellar Network as they go from hackathon win to getting funded and launching on mainnet.

Read more

👋 Kindness is contagious

Dive into this thoughtful article, cherished within the supportive DEV Community. Coders of every background are encouraged to share and grow our collective expertise.

A genuine "thank you" can brighten someone’s day—drop your appreciation in the comments below!

On DEV, sharing knowledge smooths our journey and strengthens our community bonds. Found value here? A quick thank you to the author makes a big difference.

Okay