connect method

Future<void> connect()

Implementation

Future<void> connect() async {
  print('🔌 Connecting to VM service...');
  print('  📡 $wsUrl');

  // Retry up to 5 times — VM service may not be ready immediately
  for (var attempt = 1; attempt <= 5; attempt++) {
    try {
      _vmService = await vmServiceConnectUri(wsUrl, log: null);
      final vm = await _vmService!.getVM();
      _isolateId = vm.isolates!.first.id!;

      print('✅ Connected to VM service');
      print('📱 Isolate ID: $_isolateId');

      final isolate = await _vmService!.getIsolate(_isolateId!);
      print('🩺 App name   : ${isolate.name}');
      print('🩺 App running: ${isolate.runnable}');
      return;
    } catch (e) {
      if (attempt == 5) {
        print('❌ Failed to connect after $attempt attempts: $e');
        rethrow;
      }
      print('  ⏳ Connection attempt $attempt failed — retrying in 2s...');
      await Future.delayed(const Duration(seconds: 2));
    }
  }
}