gpt4 book ai didi

dart - 是否有一种很好的方法来针对不同的环境进行不同的配置?

转载 作者:行者123 更新时间:2023-12-03 02:58:15 25 4
gpt4 key购买 nike

我创建了一个需要一些配置的应用程序。我现在的操作方式有些繁琐且乏味-如果是在本地开发,则包括“本地”配置文件(定义一些const的dart文件),如果进行部署,则包括不同的dart定义具有不同值的相同const的文件。

理想情况下,将有一种定义不同配置的方法,可以将其传递给发布服务/构建。似乎有如此明显的需求,我觉得有些东西可能已经存在,而我还没有遇到。在那儿?还是正在创作中?

最佳答案

Pub build does not support defining environment variables,但是您可以在fromEnvironment上使用 String method:

String.fromEnvironment(String name, {String defaultValue})

Returns the string value of the environment declaration name. Environment declarations are provided by the surrounding system compiling or running the Dart program. Declarations map a string key to a string value.



或使用 transformers使其“更奇特”。它将允许您自动(在pub build或pub serve上)从 pubspec.yaml或其他来源获取设置,并将其嵌入到代码中。例如 sass(它是转换器)包支持这种形式的设置:

:::yaml
transformers:
- sass:
executable: /path/to/sass # Sass executable to use
compass: true # Include compass
line-numbers: true # Include line numbers in output
style: compact # Style of generated CSS
copy-sources: true # Copy original .scss/.sass files to output directory

有了它,您几乎可以做任何事情,包括修改源代码。

关于dart - 是否有一种很好的方法来针对不同的环境进行不同的配置?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24482536/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com