withContext method

  1. @override
RequiredPick withContext(
  1. String key,
  2. Object? value
)
override

Attaches additional information which can be used during parsing. i.e the HTTP request/response including headers

Use this method to chain methods. It's pure syntax sugar. The alternative cascade operator often requires additional parenthesis

Add context at the top

pick(json)
  .withContext('apiVersion', response.getApiVersion())
  .let((pick) => Response.fromPick(pick));

Read it where required

factory Item.fromPick(RequiredPick pick) {
    final Version apiVersion = pick.fromContext('apiVersion').asVersion();
    if (apiVersion >= Version(0, 2, 0)) {
      return Item(
        color: pick("detail", "color").required().asString(),
      );
    } else {
      return Item(
        color: pick("meta-data", "variant", 0, "color").required().asString(),
      );
    }
  }

Implementation

@override
RequiredPick withContext(String key, Object? value) {
  super.withContext(key, value);
  return this;
}