Environment and servers
Override --dart-define launch arguments and switch servers at runtime with DebugEnv.
CARP apps select their environment with String.fromEnvironment (--dart-define), which is fixed at build time. DebugEnv adds a runtime override layer, persisted in SharedPreferences, so a tester can point the app at a different server, change the deployment mode or debug level, and flip feature flags without rebuilding.
How it works
Initialize early (debug only)
In your debug-only setup, load the persisted overrides before anything reads configuration: call
await DebugEnv().initialize()(optionally passing your existingSharedPreferences), then install the hook withdebugLaunchOverride = DebugEnv().overrideOf.Register the arguments
DebugEnv().registerAll([...])tells the Environment tool which arguments exist and how to edit them.Read through the hook
Replace
String.fromEnvironment('x')reads withdebugLaunchOverride?.call('x') ?? const String.fromEnvironment('x').Apply or restart
After editing, Apply runs your
onApplyhook to reconfigure the app live, and Restart relaunches the app so the new values are read from scratch.
See the Quickstart for the complete setup.
Reading values
Warning
Because the package is meant to be a dev_dependency, your production code must not call DebugEnv directly. Read through the app-local debugLaunchOverride hook instead (see Production builds). Use DebugEnv itself only inside the debug-only setup file, where the hook is installed with debugLaunchOverride = DebugEnv().overrideOf.
Once initialize() has completed, DebugEnv reads are synchronous, so you can use them inside constructors. They are convenient inside debug-only code:
CARP example: switch the CAWS server
Read the host through the hook in your CarpBackend, and expose a reconfigure() method that re-points every CAWS service:
Wire it up with onApply: () => bloc.backend.reconfigure().
Warning
Authentication tokens are server-specific. After switching servers, sign in again. The Restart action gives the cleanest slate.
EnvEntry reference
Each registered argument is described by an EnvEntry. Its type decides which editor the Environment tool renders.
| Property | Type | Required | Default | Description |
|---|---|---|---|---|
key | String | Yes | — | The --dart-define name and the storage and lookup key, for example deployment-mode. |
label | String | Yes | — | Label shown in the Environment tool. |
type | EnvValueType | No | text | text, enumeration, toggle or integer. |
options | List<String> | No | — | Allowed values when type is enumeration, rendered as a dropdown. |
fallback | String | No | — | The compile-time default, typically String.fromEnvironment(...). Shown when no override is set, and used by Revert to default. |
group | String | No | — | Optional heading for visual grouping, for example Server or Logging. |
description | String | No | — | Optional explanatory text shown under the editor. |
Editor types
Managing overrides programmatically
Note
Overrides are stored in SharedPreferences under the carp_debug_env. prefix. The Shared Preferences tool hides these keys, so they are managed only from the Environment tool; editing them in the Shared Preferences tool would not update the in-memory state.