Ashe
Ashe

Reputation: 79

Is there a way to know at compile time whether environmental variables exist in Flutter?

I would like to make sure to receive input for safe use of environmental variables in the Flutter app. Therefore, I want to make sure that the commands of 'flutter run' or 'flutter build' are not executed at runtime, but at compile time. Is there a good way?

Upvotes: 0

Views: 300

Answers (1)

Ashe
Ashe

Reputation: 79

I searched through the keyword "static assert" and solved it. This method fails when dart-define is not provided for build time.

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');
}

Upvotes: 4

Related Questions