withEnv<T> function

T withEnv<T>(
  1. T callback(),
  2. Map<String, String?> environment, {
  3. bool includeParentEnvironment = true,
})

Runs callback in a zone with its own environment variables.

Any modifications to environment variables within callback won't affect the outer environment.

By default, environment is merged with env. Any null values will remove the corresponding keys from the parent env. If includeParentEnvironment is false, environment is used as the entire child environment instead.

Implementation

T withEnv<T>(T callback(), Map<String, String?> environment,
    {bool includeParentEnvironment = true}) {
  var newEnvironment = _newMap();
  if (includeParentEnvironment) newEnvironment.addAll(env);

  for (var entry in environment.entries) {
    var value = entry.value;
    if (value == null) {
      newEnvironment.remove(entry.key);
    } else {
      newEnvironment[entry.key] = value;
    }
  }

  return runZoned(callback, zoneValues: {#_environment: newEnvironment});
}