简体   繁体   English

Flutter 项目中 Android、iOS 和 Dart 之间的共享常量

[英]Sharing constants between Android , iOS and Dart in a Flutter Project

Is there any workaround to share constant variables between the android,iOS and dart code? android、iOS 和 dart 代码之间是否有共享常量变量的解决方法? This can be especially useful when using Platform Channels as the Channel name, method names and argument key names are kept in a constants file and alternately the same file is created in kotlin and swift to match.这在使用平台通道时特别有用,因为通道名称、方法名称和参数键名称保存在常量文件中,并且在 kotlin 和 swift 中创建相同的文件以进行匹配。 Can we avoid this duplication by keeping a common file that can be shared and accessed by the 3 platforms?我们能否通过保留一个可由 3 个平台共享和访问的公共文件来避免这种重复?

  • you can use flutter run —dart-define MY_DATA=SomeString.你可以使用 flutter run —dart-define MY_DATA=SomeString。 see

  • example例子


static class AppConfig{
  static const String MY_DATA= String.fromEnvironment("MY_DATA",
      defaultValue: 'initValue');
}

void test(){
     print(AppConfig.MY_DATA);
}

Regarding unified Platform Channels usage pigeon package could be used to simplify the whole process.关于统一平台渠道使用信鸽package可以简化整个流程。 But for now, it does not resolve in particular constants sharing.但就目前而言,它并没有特别解决常量共享问题。

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

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