toJson method

String toJson()

Encodes all environment variables to a json string. This method is intended to be used in conjuction with fromJson.

You will find this method useful when spawning an isolate that depends on environment variables created by calls to DCli env property.

When creating an isolate it takes its environment variables from Platform.environment. This means that any environment variables created via DCli will not be visible to the isolate.

The way to over come this problem is to call Env().toJson() pass the resulting string to the isolate and then have the isolate call Env().fromJson() which resets the isolates environment variables.

void startIsolate() {
  var iso = waitForEx<IsolateRunner>(IsolateRunner.spawn());

  try {
     iso.run(scheduler, Env().toJson());
  } finally {
    waitForEx(iso.close());
  }
}

// This method runs in the new isolate.
void scheduler(String jsonEnvironment) {
  Env().fromJson(jsonEnvironment);
  Certbot().scheduleRenews();
}

Implementation

String toJson() {
  final envMap = <String, String>{}..addEntries(env.entries.toSet());
  return JsonEncoder(_toEncodable).convert(envMap);
}