简体   繁体   English

你如何在 Flutter/Dart 中将参数从命令行传递到 main?

[英]How do you pass arguments from command line to main in Flutter/Dart?

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/Dart 传递一些自定义参数,以便可以在main()调用中访问它们,例如:

flutter run -device [my custom arg]

So then I can access it with:那么我可以通过以下方式访问它:

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

Thank you.谢谢你。

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如果这是为了开发,你可以通过-t lib/my_alternate_main.dartflutter run来轻松切换不同的设置
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.为此添加了--dart-define=...命令行参数。

Additional key-value pairs that will be available as constants from the String.fromEnvironment, bool.fromEnvironment, int.fromEnvironment, and double.fromEnvironment constructors.其他键值对将作为 String.fromEnvironment、bool.fromEnvironment、int.fromEnvironment 和 double.fromEnvironment 构造函数中的常量提供。

For more details see Flutter 1.17 no more Flavors, no more iOS Schemas.有关更多详细信息,请参阅Flutter 1.17 不再有 Flavors,不再有 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.请注意const是必需的,并且变量名称区分大小写。

Android Studio安卓工作室

Adding command line arguments / environment variables to Android Studio Flutter project.向 Android Studio Flutter 项目添加命令行参数/环境变量。


Edit编辑

Run > Edit Configurations...运行 > 编辑配置...

or click the Configuration drop-down selector或单击配置下拉选择器

运行/调试配置选择器

Add添加

Add your arguments in Additional arguments (quotes optional if no spaces) 2. Add a descriptive name if you likeAdditional arguments添加您的参数(如果没有空格,则为可选的引号) 2. 如果您愿意,可以添加一个描述性名称

名称和配置参数

Copy复制

Click copy button to easily add more config versions as needed单击复制按钮可根据需要轻松添加更多配置版本

重复配置以添加更多

Select选择

Select your run Configs from drop down从下拉列表中选择您的运行配置

配置选择器

Use

Using your arguments in code在代码中使用你的参数

eg例如

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

使用参数

-dart-define is working in the stable channel version 1.17 -dart-define 在稳定的频道版本 1.17 中工作

from commandline从命令行

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

in for example main.dart:例如在 main.dart 中:

const MY_VAR = String.fromEnvironment('myVar', defaultValue: 'SOME_DEFAULT_VALUE');

可以使用参数--dart-entrypoint-args (简称: -a )声明main方法的参数,例如

flutter run -d linux --dart-entrypoint-args some_file.xml

I had the same problem, so I wrote a package and some instructions that can help.我遇到了同样的问题,所以我写了一个包和一些可以帮助的说明。

https://pub.dev/packages/launch_args https://pub.dev/packages/launch_args

I'm not aware of a way to pass the args via the flutter command.我不知道通过flutter命令传递参数的方法。 As far as I know, you have to first build the app via Flutter, then use the other CLIs to pass the tools.据我所知,你必须首先通过 Flutter 构建应用程序,然后使用其他 CLI 来传递工具。

Android安卓

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

iOS 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.请务必使用 Flutter 缓存工件中托管的ios-deploy版本。 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.他们肯定对该工具进行了一些调整,而不是您可以通过 Homebrew 安装的标准工具,因为我只有在使用 Flutter 的内部版本时才能使事情正常工作。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM