targetIdFor method

Future<String> targetIdFor(
  1. String? libraryUri
)

Resolves a library URI to a VM object id, defaulting to the app's root library.

Evaluating in a specific library's scope is what makes private members reachable: _localCounter is private to pages/home_page.dart, so it is invisible when evaluating against the root library, but visible when evaluating against its own library.

Implementation

Future<String> targetIdFor(String? libraryUri) async {
  _ensureConnected();
  if (libraryUri == null || libraryUri.isEmpty) return _rootLibraryId!;

  final isolate = await getIsolate();
  for (final lib in isolate.libraries ?? const <LibraryRef>[]) {
    if (lib.uri == libraryUri) {
      if (lib.id == null) {
        throw StateError('Library $libraryUri has no id.');
      }
      return lib.id!;
    }
  }
  final available = (isolate.libraries ?? const <LibraryRef>[])
      .map((l) => l.uri)
      .whereType<String>()
      .where((u) => !u.startsWith('dart:'))
      .toList();
  throw StateError(
    'No library in the isolate matches "$libraryUri".\n'
    'App libraries: ${available.join(", ")}',
  );
}