如何在Flutter/Dart中将参数从命令行传递给main? [英] How do you pass arguments from command line to main in Flutter/Dart?

查看:1193
本文介绍了如何在Flutter/Dart中将参数从命令行传递给main?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您将如何运行命令并通过Flutter/Dart传递一些自定义参数,以便随后可以在main()调用中对其进行访问,例如:

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]

这样我就可以使用:

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

谢谢.

推荐答案

无法执行此操作,因为在设备上启动应用程序时,也不会传递任何参数.

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

如果这是用于开发,则可以将-t lib/my_alternate_main.dart传递给 flutter run轻松在不同设置之间切换
其中每个备用入口点文件都使用不同的参数或初始化的全局变量使用不同的参数调用相同的应用程序代码.

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.

更新

对于

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

为此添加了--dart-define=...命令行参数.

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

其他键值对,它们可以作为常量从String.fromEnvironment,bool.fromEnvironment,int.fromEnvironment和double.fromEnvironment构造函数中获取.

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

这篇关于如何在Flutter/Dart中将参数从命令行传递给main?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆