PlayxEnv class abstract

Application configuration from .env files, flavors and --dart-define.

Call load once at startup (done by PlayxCore.bootCore when envSettings is provided), then read values synchronously:

final apiUrl = PlayxEnv.getUri('API_URL', fallback: Uri.parse('https://api.example.com'));
final retries = PlayxEnv.getInt('RETRIES', fallback: 3);

The getters are shortcuts for the same getters on values. See PlayxEnvSettings for how files and values are layered.

Security: .env files ship as plain text inside the app. Never put secrets in them.

Constructors

PlayxEnv()

Properties

hashCode → int
The hash code for this object.
no setterinherited
runtimeType → Type
A representation of the runtime type of the object.
no setterinherited

Methods

noSuchMethod(Invocation invocation) → dynamic
Invoked when a nonexistent method or property is accessed.
inherited
toString() → String
A string representation of this object.
inherited

Operators

operator ==(Object other) → bool
The equality operator.
inherited

Static Properties

all → Map<String, String>
Every loaded value except secure ones, safe to log or display.
no setter
environment → String?
The environment resolved by load (e.g. prod), if any.
no setter
isLoaded → bool
Whether load, loadFromString or setMockValues has been called.
no setter
loadedFiles → List<String>
The files that were found and loaded, in order.
no setter
values → EnvValues
The loaded values. Empty, with a logged warning, before load.
no setter

Static Methods

containsKey(String key) → bool
Whether key has a value.
dispose() → Future<void>
Clears all values. load must be called again before use.
getBool(String key, {bool fallback = false}) → bool
Returns the value of key as a bool, or fallback.
getDouble(String key, {double fallback = -1}) → double
Returns the value of key as a double, or fallback.
getDuration(String key, {required Duration fallback}) → Duration
Returns the value of key as a Duration, or fallback.
getEnum<T extends Enum>(String key, List<T> enumValues, {required T fallback}) → T
Returns the enum value of values matching the value of key, or fallback.
getInt(String key, {int fallback = -1}) → int
Returns the value of key as an int, or fallback.
getList<T>(String key, {List<T> fallback = const [], T fromJson(dynamic json)?, String separator = ','}) → List<T>
Returns the value of key as a list, or fallback.
getMap(String key, {Map<String, dynamic> fallback = const {}}) → Map<String, dynamic>
Returns the value of key parsed as a JSON object, or fallback.
getNum(String key, {num fallback = -1}) → num
Returns the value of key as a num, or fallback.
getObject<T>(String key, {required T fromJson(dynamic json), required T fallback}) → T
Returns the value of key converted with fromJson, or fallback.
getString(String key, {String fallback = ''}) → String
Returns the value of key, or fallback if it isn't set.
getUri(String key, {required Uri fallback}) → Uri
Returns the value of key as a Uri, or fallback.
isSecure(String key) → bool
Whether key holds a secure value (see PlayxEnvSettings.secureEnv).
load([PlayxEnvSettings settings = const PlayxEnvSettings()]) → Future<void>
Loads the configuration described by settings.
loadFromString(String content, {Map<String, String> mergeWith = const {}, Map<String, String> dartDefines = const {}, List<String> requiredKeys = const [], bool usePlayxDefines = true, Parser parser = const Parser()}) → void
Loads values from content in .env format, then mergeWith, PLAYX_DEFINES (if usePlayxDefines) and dartDefines. Useful in tests, or for config fetched at runtime.
maybeGetBool(String key) → bool?
Returns the value of key as a bool: true/1 or false/0 (case-insensitive). null if it isn't set or is anything else.
maybeGetDouble(String key) → double?
Returns the value of key as a double, or null if it isn't set or isn't a number.
maybeGetDuration(String key) → Duration?
Returns the value of key as a Duration, or null if it isn't set or can't be parsed.
maybeGetEnum<T extends Enum>(String key, List<T> enumValues) → T?
Returns the enum value of values whose name matches the value of key (case-insensitive), or null.
maybeGetInt(String key) → int?
Returns the value of key as an int, or null if it isn't set or isn't a number. Decimals are truncated.
maybeGetList<T>(String key, {T fromJson(dynamic json)?, String separator = ','}) → List<T>?
Returns the value of key as a list, or null if it isn't set or an item can't be converted.
maybeGetMap(String key) → Map<String, dynamic>?
Returns the value of key parsed as a JSON object, or null.
maybeGetNum(String key) → num?
Returns the value of key as a num, or null if it isn't set or isn't a number.
maybeGetObject<T>(String key, {required T fromJson(dynamic json)}) → T?
Returns the value of key parsed as JSON and converted with fromJson, or null if it isn't set, isn't valid JSON, or fromJson throws.
maybeGetString(String key) → String?
Returns the value of key, or null if it isn't set.
maybeGetUri(String key) → Uri?
Returns the value of key as a Uri, or null if it isn't set or can't be parsed.
setMockValues(Map<String, String> values) → void
Replaces all values with values, for tests.