mirror of
https://github.com/flutter/flutter.git
synced 2026-02-20 02:29:02 +08:00
I started on a refactoring of https://github.com/flutter/flutter/issues/147666, and was frustrated with the JSON decoding of `gn desc --format=json`, so I used a similar pattern from my micro-json library (https://pub.dev/packages/jsonut) and encapsulated it as an extension type. My favorite is the `JsonObject.map` function which replaces: ```dart /// Construct a RunTarget from a JSON map. factory RunTarget.fromJson(Map<String, Object> map) { final List<String> errors = <String>[]; final String name = stringOfJson(map, _nameKey, errors)!; final String id = stringOfJson(map, _idKey, errors)!; final String targetPlatform = stringOfJson(map, _targetPlatformKey, errors)!; if (errors.isNotEmpty) { throw FormatException('Failed to parse RunTarget: ${errors.join('\n')}'); } return RunTarget._(name, id, targetPlatform); } ``` ... with: ```dart /// Construct a RunTarget from a JSON map. factory RunTarget.fromJson(Map<String, Object> map) { return JsonObject(map).map((JsonObject json) => RunTarget._( json.string(_nameKey), json.string(_idKey), json.string(_targetPlatformKey), )); } ```