是否有办法知道在编译时环境变量是否存在于Flutter?



我想确保在Flutter应用程序中安全使用环境变量接收输入。因此,我想确保'flutter run'或'flutter build'的命令不是在运行时执行,而是在编译时执行。有什么好办法吗?

我搜索了关键词"静态断言";然后解出来了。当构建时未提供dart-define时,此方法将失败。

class StaticAssert {
const StaticAssert(bool condition, [String message = "Assertion Failed"])
: assert(condition, message);
}
class DartDefine {
@StaticAssert(environment == "development" || environment == "production",
"--dart-define environment=<value> must be one of development or production")
static const String environment = String.fromEnvironment('environment');
}

最新更新