getSnapshot static method Null safety

Future<HeapSnapshotGraph> getSnapshot(
  1. VmService service,
  2. IsolateRef isolate
)

Requests a heap snapshot for a given isolate and builds a HeapSnapshotGraph.

Note: this method calls VmService.streamListen and VmService.streamCancel on EventStreams.kHeapSnapshot.

Implementation

static Future<HeapSnapshotGraph> getSnapshot(
    VmService service, IsolateRef isolate) async {
  await service.streamListen(EventStreams.kHeapSnapshot);

  final completer = Completer<HeapSnapshotGraph>();
  final chunks = <ByteData>[];
  late StreamSubscription streamSubscription;
  streamSubscription = service.onHeapSnapshotEvent.listen((e) async {
    chunks.add(e.data!);
    if (e.last!) {
      await service.streamCancel(EventStreams.kHeapSnapshot);
      await streamSubscription.cancel();
      completer.complete(HeapSnapshotGraph.fromChunks(chunks));
    }
  });

  await service.requestHeapSnapshot(isolate.id!);
  return completer.future;
}