Trace.decode constructor

Trace.decode(
  1. Uint8List bytes
)

Reads a trace written by encode.

Implementation

factory Trace.decode(Uint8List bytes) {
  final data = ByteData.sublistView(bytes);
  for (var i = 0; i < _magic.length; i++) {
    if (bytes.length <= i || bytes[i] != _magic[i]) {
      throw const FormatException('not a flutter3d trace: no F3DTRACE magic');
    }
  }
  final version = data.getUint32(8, Endian.little);
  if (version != formatVersion) {
    throw FormatException(
      'a trace of format $version; this build reads $formatVersion',
    );
  }
  final headerLength = data.getUint32(12, Endian.little);
  final header = asMap(
    jsonDecode(utf8.decode(bytes.sublist(16, 16 + headerLength))),
  );
  final blob = TraceBlobReader(bytes.sublist(16 + headerLength));
  return Trace(<TraceEvent>[
    for (final e in (header['events']! as List<Object?>).map(asMap))
      TraceEvent.fromJson(e, blob),
  ], metadata: asMap(header['metadata'] ?? <String, Object?>{}));
}