50

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.

Miguel Ruivo
  • 16,035
  • 7
  • 57
  • 87

6 Answers6

53

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.

Günter Zöchbauer
  • 623,577
  • 216
  • 2,003
  • 1,567
  • 1
    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. – Miguel Ruivo Mar 05 '19 at 14:37
  • 1
    Worth mention that this works on `flutter build` too. – Ricardo Gonçalves Nov 26 '19 at 17:51
  • @Günter Zöchbauer, plz give an example or link to `--dart-define` – Saahithyan Vigneswaran Apr 13 '20 at 05:32
  • 2
    --dart-define got reverted https://github.com/flutter/flutter/pull/52041 and I could not get it to work – m1416 Apr 17 '20 at 14:36
  • @m1416 thanks for the update. I didn't have a chance yet to try it myself. – Günter Zöchbauer Apr 17 '20 at 14:40
  • is it possible to use --dart-define in run/debug configuration? – Maurice Raguse Jun 09 '20 at 08:53
  • yeah but String.fromEnvironment('myVar', defaultValue: 'SOME_DEFAULT_VALUE'). returns the defaultValue and the arg parameter from the main is empty – Maurice Raguse Jun 09 '20 at 09:26
  • @FPerroch as far as I know this was re-added again since. – Günter Zöchbauer Jul 21 '20 at 20:22
  • 1
    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 work – sultanmyrza Jul 26 '20 at 15:20
  • @SultanmyrzaKasymbekov I just tried it and it's working fine (Flutter 1.21.0-1.0.pre) – Günter Zöchbauer Jul 26 '20 at 16:32
  • @Raegtime sorry, my last (deleted) response contained the wrong link. See also https://github.com/flutter/flutter/issues/55870 – Günter Zöchbauer Jul 26 '20 at 16:33
  • 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 ? – Cristiano Aug 07 '20 at 20:52
  • @Cristiano that's exactly what it is for. You can pass it multiple times for different values – Günter Zöchbauer Aug 07 '20 at 20:56
  • 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? – Cristiano Aug 07 '20 at 21:07
  • 1
    @Cristiano 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. – Günter Zöchbauer Aug 08 '20 at 04:43
  • 1
    *note:* for each argument add dart-define: `--dart-define="var1=value1" --dart-define="var2=value2"` – Kirill Karmazin Jan 12 '22 at 13:55
30

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

Baker
  • 24,730
  • 11
  • 100
  • 106
19

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
Janux
  • 841
  • 1
  • 9
  • 19
  • 2
    This is the only true solution to pass command line arguments, and they are equivalent to the arguments passed to the compiled exe file. – Mabsten May 02 '21 at 11:39
  • 1
    **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. – Mitrakov Artem Oct 28 '22 at 17:17
  • 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. – Moshe Yamini Mar 16 '23 at 10:45
  • 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. – mzimmermann May 31 '23 at 04:25
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');
Robin Manoli
  • 2,162
  • 2
  • 25
  • 30
  • 1
    Did you tried on Flutter (Channel master, 1.19.0-2.0.pre, on Mac OS X 10.15.5) didn’t worked for me – sultanmyrza Jul 30 '20 at 03:11
  • How to do this from the Android Studio build (clicking the green Run arrow) – user3808307 Oct 27 '20 at 22:48
  • 1
    @user3808307 https://stackoverflow.com/a/64686348/2301224 – Baker Nov 04 '20 at 21:55
  • @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](https://medium.com/@tatsu.ukraine/what-you-should-know-before-you-upgrade-flutter-in-your-project-with-compile-time-variables-3ec3d2e9ba79) may still be relevant. – hicnar Apr 08 '21 at 11:33
1

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.

Jeff Peiffer
  • 657
  • 6
  • 4
1

@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]
}
Moshe Yamini
  • 608
  • 9
  • 13