How do you pass arguments from command line to main in Flutter/Dart?
Asked Answered
P

6

53

How would you run a command and pass some custom arguments with Flutter/Dart so they can then be accessed in the main() call such as:

flutter run -device [my custom arg]

So then I can access it with:

void main(List<String> args) {
  print(args.toString());
}

Thank you.

Puri answered 5/3, 2019 at 13:45 Comment(1)
can you please re-evaluate the accepted answer. The one from @Janux seems to be a correct oneNereen
P
27

The arguments for the main method can be declared with the parameter --dart-entrypoint-args (short: -a), e.g.

flutter run -d linux --dart-entrypoint-args some_file.xml
Pacifist answered 3/1, 2021 at 15:57 Comment(5)
This is the only true solution to pass command line arguments, and they are equivalent to the arguments passed to the compiled exe file.Placoid
NOTE this is the only correct answer here, if you want to pass command line arguments. All other answers are primarily about ENV variables, which is completely different question.Onfroi
This is the correct answer. I thought at first I need to create an XML file with my args. But then I realized that this is an example to pass the some_file.xml arg.Planchet
While this is technically the correct answer, it is not portable. According to flutter help run, the --dart-entrypoint-args is only supported on desktop. So for any practical portable solution, a workaround using --dart-define is needed.Scherle
Updated the accepted answer to this one, since seems to be the more correct as of now.Puri
O
57

There is no way to do that, because when you start an app on your device there are also no parameters that are passed.

If this is for development, you can pass -t lib/my_alternate_main.dart to flutter run to easily switch between different settings
where each alternate entry-point file calls the same application code with different parameters or with differently initialized global variables.

Update

For

  • flutter run
  • flutter build apk
  • flutter build ios
  • flutter drive

the --dart-define=... command line parameter was added for that purpose.

Additional key-value pairs that will be available as constants from the String.fromEnvironment, bool.fromEnvironment, int.fromEnvironment, and double.fromEnvironment constructors.

For more details see Flutter 1.17 no more Flavors, no more iOS Schemas. Command argument that changes everything

Example

const t = String.fromEnvironment("TEST");
flutter run --dart-define="TEST=from command line"

Be aware that const is required and that the variable name is case sensitive.

Olly answered 5/3, 2019 at 13:58 Comment(17)
I've been using it with environment variables for CI so it can run tests for both iOS and Android. Just wanted to make sure if it was supported or not, if so, I would use main() args instead. Thank you.Puri
Worth mention that this works on flutter build too.Cayser
@Günter Zöchbauer, plz give an example or link to --dart-defineKeirakeiser
--dart-define got reverted github.com/flutter/flutter/pull/52041 and I could not get it to workRothberg
@Rothberg thanks for the update. I didn't have a chance yet to try it myself.Mitis
is it possible to use --dart-define in run/debug configuration?Modigliani
yeah but String.fromEnvironment('myVar', defaultValue: 'SOME_DEFAULT_VALUE'). returns the defaultValue and the arg parameter from the main is emptyModigliani
@FPerroch as far as I know this was re-added again since.Mitis
As of today 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 workBoutique
@SultanmyrzaKasymbekov I just tried it and it's working fine (Flutter 1.21.0-1.0.pre)Mitis
@Raegtime sorry, my last (deleted) response contained the wrong link. See also github.com/flutter/flutter/issues/55870Mitis
Suppose I use --dart-define in a build apk in order to set the current URL of an API backend (that is different from dev). it would be possible to store that info, so it can be used by the app running in a device ?Quattrocento
@Quattrocento that's exactly what it is for. You can pass it multiple times for different valuesMitis
Günter, ok that I understood, thanks. So, am I right to say that if I set a const MYV = String.fromEnvironment("TEST"), then the MYV const will be compiled and will retain the value 5 passed by --dart-define=TEST=5 after the apk be generated?Quattrocento
@Quattrocento yes, it's easy enough to try and see for yourself ;D Ensure you have the latest Flutter version. This feature is rather new but AFAIK it's already in stable. I only tried in dev myself.Mitis
note: for each argument add dart-define: --dart-define="var1=value1" --dart-define="var2=value2"Dogcart
Thanks @GünterZöchbauer a huge help for the 'const' keyword, I was stuck for an hour but didn't find this anywhereSaga
W
32

Android Studio

Adding command line arguments / environment variables to Android Studio Flutter project.


Edit

Run > Edit Configurations...

or click the Configuration drop-down selector

run/debug config selector

Add

Add your arguments in Additional arguments (quotes optional if no spaces) 2. Add a descriptive name if you like

Name and config arguments

Copy

Click copy button to easily add more config versions as needed

Duplicate config to add more

Select

Select your run Configs from drop down

Config Selector

Use

Using your arguments in code

e.g.

const String version = String.fromEnvironment('VERSION');

Use Arguments

Wizard answered 4/11, 2020 at 19:16 Comment(0)
P
27

The arguments for the main method can be declared with the parameter --dart-entrypoint-args (short: -a), e.g.

flutter run -d linux --dart-entrypoint-args some_file.xml
Pacifist answered 3/1, 2021 at 15:57 Comment(5)
This is the only true solution to pass command line arguments, and they are equivalent to the arguments passed to the compiled exe file.Placoid
NOTE this is the only correct answer here, if you want to pass command line arguments. All other answers are primarily about ENV variables, which is completely different question.Onfroi
This is the correct answer. I thought at first I need to create an XML file with my args. But then I realized that this is an example to pass the some_file.xml arg.Planchet
While this is technically the correct answer, it is not portable. According to flutter help run, the --dart-entrypoint-args is only supported on desktop. So for any practical portable solution, a workaround using --dart-define is needed.Scherle
Updated the accepted answer to this one, since seems to be the more correct as of now.Puri
P
7

-dart-define is working in the stable channel version 1.17

from commandline

flutter run --dart-define=myVar="some value"

in for example main.dart:

const MY_VAR = String.fromEnvironment('myVar', defaultValue: 'SOME_DEFAULT_VALUE');
Pulitzer answered 12/5, 2020 at 8:15 Comment(4)
Did you tried on Flutter (Channel master, 1.19.0-2.0.pre, on Mac OS X 10.15.5) didn’t worked for meBoutique
How to do this from the Android Studio build (clicking the green Run arrow)Ochs
@Ochs https://mcmap.net/q/168065/-how-do-you-pass-arguments-from-command-line-to-main-in-flutter-dartWizard
@SultanmyrzaKasymbekov Exactly, works on Android, but on android (as opposed to iOS) you can have your main method defined in a dart file that does not have to be named main.dart. On iOS no dart defines are passed into the environment. I think this article link may still be relevant.Eudoca
P
3

@Janux already answered the right answer.

I just want to make a simple example:

flutter run -d linux -a my_argument
void main(List<String> args) {
  print(args); // flutter: [my_argument]
}
Planchet answered 16/3, 2023 at 10:54 Comment(0)
I
2

I had the same problem, so I wrote a package and some instructions that can help.

https://pub.dev/packages/launch_args

I'm not aware of a way to pass the args via the flutter command. As far as I know, you have to first build the app via Flutter, then use the other CLIs to pass the tools.

Android

adb -s $DEVICE_ID shell am start \
  -n $ANDROID_PACKAGE/$ANDROID_ACTIVITY \
  -ez [arg name] [value] \
  -ez [arg name2] [value 2] \
  ...

iOS

$FLUTTER_HOME/bin/cache/artifacts/ios-deploy/ios-deploy --id $DEVICE_ID \
  --bundle build/ios/iphoneos/Runner.app \
  --debug \
  --args [arg name] [arg value] [arg name2] [arg value2] ...

Be sure to use the version of ios-deploy that's hosted in Flutter's cached artifacts. They must have made some tweaks to that tool vs the standard one that you can install via Homebrew because I could only get things working when I used Flutter's internal version.

Iscariot answered 23/12, 2020 at 16:50 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.