DataSourceTrace.fromJson constructor
Reads what toJson wrote. Throws a DataSourceTraceFormatException
when steps or values are missing, differ in number, or an entry is not
a step number and a map. The order of steps is not checked.
Implementation
factory DataSourceTrace.fromJson(Map<String, Object?> json) {
final rawSteps = json['steps'];
final rawValues = json['values'];
if (rawSteps is! List || rawValues is! List) {
throw const DataSourceTraceFormatException(
'the trace has no steps or no values',
);
}
if (rawSteps.length != rawValues.length) {
throw const DataSourceTraceFormatException(
'the trace has a different number of steps and values',
);
}
final steps = <int>[];
final values = <Map<String, Object?>>[];
for (var i = 0; i < rawSteps.length; i++) {
final step = rawSteps[i];
final value = rawValues[i];
if (step is! num || value is! Map) {
throw const DataSourceTraceFormatException(
'an entry is not a step number and a value map',
);
}
steps.add(step.toInt());
values.add(Map<String, Object?>.unmodifiable(value));
}
return DataSourceTrace._parts(steps, values);
}