# -*- coding: utf-8 -*- import sys path = r'C:\Users\jsmbz\Documents\flutter-app-02\lib\features\home\presentation\widgets\map\testmap_pages.dart' with open(path, 'r', encoding='utf-8') as f: content = f.read() # Find the "存在任务" branch that just returns without saving taskId # We'll replace the "return;" in that branch with code that does async requery # But fold callbacks can't be async... # Instead, let's find the fold end and add async handler after it # Find: "\n );\n } catch (e) {" old_pat = '\n );\n } catch (e) {' idx = content.find(old_pat) if idx < 0: print('Pattern not found, trying variants...') # Try with different whitespace old_pat = ' );\n } catch (e) {' idx = content.find(old_pat) if idx < 0: print('Still not found') sys.exit(1) print(f'Found at index {idx}') # Insert code between the fold close and the catch # We'll restructure to handle needRequery insert_code = ''' if (needRequery) { debugPrint('设备已有任务,重新查询任务池...'); await taskCubit.fetchAndFilterTask(deviceId); await Future.delayed(const Duration(milliseconds: 300)); final existingTasks = taskCubit.state.activeTasks; if (existingTasks.isNotEmpty) { final existingTask = existingTasks.first; taskCubit.selectTask(existingTask); debugPrint('复用已有任务 #${existingTask.id},taskId已保存'); _showPageToast(message: "设备正在作业中", type: ToastType.success); _saveDataToLocal(); return; } _showPageToast(message: "设备正在作业中", type: ToastType.warn); return; } } catch (e) {''' # Now find and fix the fold callback to set needRequery flag # Replace the "存在任务" branch old_branch = '''if (failMsg.contains('存在任务') || failMsg.contains('already exists')) { debugPrint('⚠️ [开始作业] 设备已有任务,保留作业状态'); _showPageToast(message: "设备正在作业中", type: ToastType.warn); // 🔥 不重置 _workStatus,按钮保持可见 return;''' new_branch = '''if (failMsg.contains('存在任务') || failMsg.contains('already exists')) { needRequery = true; return;''' branch_idx = content.find(old_branch) if branch_idx < 0: # Try without the debug print variation old_branch = '''if (failMsg.contains('存在任务') || failMsg.contains('already exists')) { debugPrint''' branch_idx = content.find(old_branch) if branch_idx < 0: print('Branch pattern not found') else: print(f'Branch found at {branch_idx} (partial match)') else: print(f'Branch found at {branch_idx}') # Do the branch replacement content = content[:branch_idx] + new_branch + content[branch_idx + len(old_branch):] # Also add needRequery var declaration before fold # Find " result.fold(" and add var before it fold_marker = ' result.fold(' fold_idx = content.find(fold_marker) if fold_idx >= 0: # Check if needRequery is already declared check = content.find('needRequery', fold_idx - 50, fold_idx) if check < 0: content = content[:fold_idx] + ' var needRequery = false;\n' + content[fold_idx:] print('Added needRequery declaration') # Now find and replace the fold end pattern again (after content modifications) old_pat = '\n );\n } catch (e) {' idx = content.find(old_pat) if idx < 0: print('Fold end pattern not found after modifications') sys.exit(1) content = content[:idx + len('\n );')] + insert_code + content[idx + len(old_pat):] with open(path, 'w', encoding='utf-8', newline='') as f: f.write(content) print('DONE')