如何确定我运行项目的环境?(该问题涉及IT技术)

3

有没有可能确定我运行项目的环境?

flutter run --flavor prod 
flutter run --flavor dev

dart side

  @override
  void initState() {

    //THIS SHOULD BE WORK BOTH IOS AND ANDORID

    const bool isProduction = bool.fromEnvironment('com.demo.store.prod');
    if(isProduction) {
      print('it is prod');
    } else {
      print('it is not prod');//always print this line
    }
    super.initState();
  }

app/build.gradle - Android端

productFlavors {
    dev {
        dimension "app"
        versionCode 1
        versionName "1.0.0"
    }
    prod {
        dimension "app"
        applicationId "com.demo.store.prod"
        versionCode 2
        versionName "1.0.0"
    }
}

https://flutter.dev/docs/deployment/flavors - user10539074
我已经关注了 - undefined
如果你已经知道为什么不将其拆分为 main_dev.dart 和 main_prod.dart? - user10539074
@GenchiGenbutsu 我尝试了,但是出现了这个错误 https://stackoverflow.com/questions/60945064/arguments-of-a-constant-creation-must-be-constant-expressions-on-flutter - undefined
1个回答

0

幸运的是,Flutter不会将Flavors中的任何信息注入到Dart代码中。Flutter提供的最大功能是通过以下代码行判断是否为发布版本:

const bool isProduction = bool.fromEnvironment('dart.vm.product');

此外,你可以使用类似package_info的包来获取应用程序的信息。

但是,如果你对如何配置多个环境感兴趣,我建议你使用编译时变量。你可以在flutter runflutter build命令中使用--dart-define关键字来指定它们。

完成后,提供的键值对将在你的Dart和本地层中可用。你可以使用它们来指定应用程序前缀,例如。你可以在这篇文章中了解更多关于如何在Gradle和iOS Schemes中获取它们的信息。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接