58

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.

1
  • can you please re-evaluate the accepted answer. The one from @Janux seems to be a correct one Commented Jan 13, 2024 at 0:56

8 Answers 8

63

Update

This seems to have changed. See this answer instead https://stackoverflow.com/a/65551846/217408

Original

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.

Sign up to request clarification or add additional context in comments.

17 Comments

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.
Worth mention that this works on flutter build too.
--dart-define got reverted github.com/flutter/flutter/pull/52041 and I could not get it to work
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
@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.
|
35

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

6 Comments

This is the only true solution to pass command line arguments, and they are equivalent to the arguments passed to the compiled exe file.
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.
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.
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.
Updated the accepted answer to this one, since seems to be the more correct as of now.
|
33

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

Comments

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');

4 Comments

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
How to do this from the Android Studio build (clicking the green Run arrow)
@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.
6

@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]
}

Comments

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.

Comments

1

After Flutter 3.7 we can store all the API keys inside a JSON file and pass it to a new --dart-define-from-file flag from the command line.

Create a json file with any name ie api-keys.json

{
  "API_KEY": "a1b2c33d4e5f6g7h8i9jakblc",
  "STRIPE_PUBLISHABLE_KEY": "pk_test_aposdjpa309u2n230ibt23908g"
}

you can get the value against any key as follow

const apiKey = String.fromEnvironment('API_KEY');
if (apiKey.isEmpty) {
  throw AssertionError('API_KEY is not set');
}

To run the app with json file run the following command

flutter run --dart-define-from-file=api-keys.json

You can define multiple launch configurations with different API keys if needed ie api-keys.dev.json and api-keys.prod.json.

If you are using VSCode you can add args in the launch.json file as follows

{
  "version": "1.0.0",
  "configurations": [
    {
      "name": "Launch",
      "request": "launch",
      "type": "dart",
      "program": "lib/main.dart",
      "args": [
        "--dart-define-from-file",
        "api-keys.dev.json"
      ]
    }
  ]
}

If you are using Android Studio you can pass this file as an additional run argument as follow

enter image description here

Comments

0

looking at this in 2025, the dart docs have an entry for Supplying arguments to main().

one key detail is putting args for main after the name of the package that main lives in:

When you're running the main program for the current package, add the package name. Here's an example of running bin/foo.dart with arguments while you're in the top directory of the foo package:

dart run foo arg1 arg2

which, with a main method like this in bin/foo.dart:

void main(List<String> arguments) {
  print('arguments received:');
  print(arguments.join('\n'));
}

would yield the following commandline output:

$ dart run foo arg1 arg2
Building package executable...
Built foo:foo.
arguments received:
arg1
arg2

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.