93 lines
3.2 KiB
Python
93 lines
3.2 KiB
Python
|
|
import re
|
||
|
|
|
||
|
|
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()
|
||
|
|
|
||
|
|
# Fix 1: Add var failed = false and if(failed) return, plus else branch for needRequery
|
||
|
|
old_block = """ var needRequery = false;
|
||
|
|
result.fold(
|
||
|
|
(failure) {
|
||
|
|
final failMsg = failure.toString();
|
||
|
|
if (failMsg.contains('存在任务') || failMsg.contains('already exists')) {
|
||
|
|
needRequery = true;
|
||
|
|
return;
|
||
|
|
}
|
||
|
|
_showPageToast(message: '作业启动失败', type: ToastType.error);
|
||
|
|
taskCubit.clearCurrentTask();
|
||
|
|
return;
|
||
|
|
},
|
||
|
|
(taskId) {
|
||
|
|
taskCubit.updateCurrentTaskId(taskId);
|
||
|
|
},
|
||
|
|
);
|
||
|
|
|
||
|
|
if (needRequery) {
|
||
|
|
await taskCubit.fetchAndFilterTask(deviceId);
|
||
|
|
await Future.delayed(const Duration(milliseconds: 300));
|
||
|
|
final existingTasks = taskCubit.state.activeTasks;
|
||
|
|
if (existingTasks.isNotEmpty) {
|
||
|
|
taskCubit.selectTask(existingTasks.first);
|
||
|
|
}
|
||
|
|
return;
|
||
|
|
}"""
|
||
|
|
|
||
|
|
new_block = """ var needRequery = false;
|
||
|
|
var failed = false;
|
||
|
|
result.fold(
|
||
|
|
(failure) {
|
||
|
|
final failMsg = failure.toString();
|
||
|
|
if (failMsg.contains('存在任务') || failMsg.contains('already exists')) {
|
||
|
|
needRequery = true;
|
||
|
|
return;
|
||
|
|
}
|
||
|
|
failed = true;
|
||
|
|
debugPrint('[开始作业] 创建失败: $failMsg');
|
||
|
|
_showPageToast(message: '作业启动失败', type: ToastType.error);
|
||
|
|
taskCubit.clearCurrentTask();
|
||
|
|
return;
|
||
|
|
},
|
||
|
|
(taskId) {
|
||
|
|
taskCubit.updateCurrentTaskId(taskId);
|
||
|
|
},
|
||
|
|
);
|
||
|
|
|
||
|
|
if (failed) return;
|
||
|
|
if (needRequery) {
|
||
|
|
await taskCubit.fetchAndFilterTask(deviceId);
|
||
|
|
await Future.delayed(const Duration(milliseconds: 300));
|
||
|
|
final existingTasks = taskCubit.state.activeTasks;
|
||
|
|
if (existingTasks.isNotEmpty) {
|
||
|
|
taskCubit.selectTask(existingTasks.first);
|
||
|
|
} else {
|
||
|
|
debugPrint('[开始作业] needRequery 后仍无活跃任务');
|
||
|
|
_showPageToast(message: '未找到活跃任务', type: ToastType.warn);
|
||
|
|
}
|
||
|
|
return;
|
||
|
|
}"""
|
||
|
|
|
||
|
|
if old_block in content:
|
||
|
|
content = content.replace(old_block, new_block)
|
||
|
|
print("Block replaced successfully")
|
||
|
|
else:
|
||
|
|
print("Block NOT FOUND in file!")
|
||
|
|
# Try tab indentation
|
||
|
|
old_block_tabs = old_block.replace(' ', '\t\t\t\t')
|
||
|
|
if old_block_tabs in content:
|
||
|
|
content = content.replace(old_block_tabs, new_block.replace(' ', '\t\t\t\t'))
|
||
|
|
print("Block replaced with TABS")
|
||
|
|
else:
|
||
|
|
# Find the unique line
|
||
|
|
for i, line in enumerate(content.split('\n')):
|
||
|
|
if 'var needRequery = false;' in line and 'result.fold(' in content.split('\n')[i+1]:
|
||
|
|
print(f"Found at line {i+1}: {repr(line)}")
|
||
|
|
break
|
||
|
|
else:
|
||
|
|
print("Could not find needRequery line")
|
||
|
|
|
||
|
|
with open(path, 'w', encoding='utf-8', newline='') as f:
|
||
|
|
f.write(content)
|
||
|
|
|
||
|
|
print("Done")
|