55// This code is directly based on src/io/flutter/inspector/EvalOnDartLibrary.java
66// If you add a method to this class you should also add it to EvalOnDartLibrary.java
77import 'dart:async' ;
8- import 'dart:core' hide Error;
98import 'dart:core' as core;
9+ import 'dart:core' hide Error;
1010import 'dart:math' ;
1111
1212import 'package:flutter/foundation.dart' ;
1313import 'package:logging/logging.dart' ;
1414import 'package:meta/meta.dart' ;
15- import 'package:vm_service/vm_service.dart' hide Error;
1615import 'package:vm_service/vm_service.dart' as vm_service;
16+ import 'package:vm_service/vm_service.dart' hide Error;
1717
1818import '../utils/auto_dispose.dart' ;
1919import 'service_manager.dart' ;
@@ -429,6 +429,22 @@ class EvalOnDartLibrary extends DisposableController
429429 await safeEval (
430430 '() async {'
431431 ' final reader = widgetInspectorService.toObject("$readerId ", "$readerGroup ") as List;'
432+ ' /* Keep a strong reference to `reader` in the target app to prevent it'
433+ ' from being garbage collected by Chrome/VM before the future resolves.'
434+ ' Without this, the reader is only weakly referenced by the inspector'
435+ ' service and is aggressively GCed, causing a TypeError/TimeoutException'
436+ ' or failing the assertion that the retrieved result length is 1 or 2.'
437+ ' We use Future.delayed in a loop instead of Timer because Future is in'
438+ ' dart:core and guaranteed to be resolved without requiring dart:async. */'
439+ ' bool isDone = false;'
440+ ' () async {'
441+ ' int bufferTicks = 0;'
442+ ' /* Stop pinning after a 1-second buffer when the future has completed. */'
443+ ' while (!isDone && ++bufferTicks <=20) {'
444+ ' final _ = reader;'
445+ ' await Future.delayed(const Duration(milliseconds: 50));'
446+ ' }'
447+ ' }();'
432448 ' try {'
433449 // Cast as dynamic so that it is possible to await Future<void>
434450 ' dynamic result = ($expression ) as dynamic;'
@@ -437,6 +453,7 @@ class EvalOnDartLibrary extends DisposableController
437453 ' reader.add(err);'
438454 ' reader.add(stack);'
439455 ' } finally {'
456+ ' isDone = true;'
440457 ' postEvent("future_completed", {"future_id": $futureId , "client_id": $_clientId });'
441458 ' }'
442459 '}()' ,
0 commit comments