解决接口返回值类型不匹配
This commit is contained in:
@@ -134,37 +134,53 @@ class PathRepositoryImpl implements PathRepository {
|
|||||||
required String workName,
|
required String workName,
|
||||||
}) async {
|
}) async {
|
||||||
final timestamp = DateTime.now().millisecondsSinceEpoch;
|
final timestamp = DateTime.now().millisecondsSinceEpoch;
|
||||||
final url =
|
final url = Uri.parse(
|
||||||
Uri.parse(
|
'https://serviceri.satabot.com/iot/workRecord/selectByWorkName',
|
||||||
'https://serviceri.satabot.com/iot/workRecord/selectByWorkName',
|
).replace(
|
||||||
).replace(
|
queryParameters: {
|
||||||
queryParameters: {
|
'workName': workName,
|
||||||
'workName': workName,
|
'_t': timestamp.toString(),
|
||||||
'_t': timestamp.toString(), // 防缓存
|
},
|
||||||
},
|
);
|
||||||
);
|
print('请求URL: $url');
|
||||||
print('请求URL: $url'); // 调试输出完整URL,检查参数是否正确附加
|
|
||||||
|
|
||||||
try {
|
try {
|
||||||
final response = await http.get(url);
|
final response = await http.get(url);
|
||||||
|
|
||||||
if (response.statusCode == 200) {
|
if (response.statusCode == 200) {
|
||||||
final data = jsonDecode(response.body) as Map<String, dynamic>;
|
final data = jsonDecode(response.body) as Map<String, dynamic>;
|
||||||
print('响应数据: $data'); // 调试输出响应数据,检查结构和内容
|
print('响应数据: $data');
|
||||||
|
|
||||||
if (data['code'] == 200 && data.containsKey('data')) {
|
if (data['code'] == 200 && data.containsKey('data')) {
|
||||||
final List<dynamic> records = data['data'];
|
final dynamic rawData = data['data'];
|
||||||
return List<Map<String, dynamic>>.from(records);
|
|
||||||
|
// 🔧 关键修复:安全转换为 List
|
||||||
|
|
||||||
|
List<Map<String, dynamic>> records;
|
||||||
|
if (rawData is List) {
|
||||||
|
records = rawData.map((e) {
|
||||||
|
if (e is Map) {
|
||||||
|
return Map<String, dynamic>.from(e);
|
||||||
|
}
|
||||||
|
throw Exception('List item is not a Map: ${e.runtimeType}');
|
||||||
|
}).toList();
|
||||||
|
} else if (rawData is Map) {
|
||||||
|
records = [Map<String, dynamic>.from(rawData)];
|
||||||
|
} else {
|
||||||
|
throw Exception('Unexpected data type for "data": ${rawData.runtimeType}');
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
return records;
|
||||||
} else {
|
} else {
|
||||||
throw Exception('API error: ${data['msg'] ?? 'Unknown'}');
|
throw Exception('API error: ${data['msg'] ?? 'Unknown'}');
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
throw Exception(
|
throw Exception('HTTP ${response.statusCode}: ${response.reasonPhrase}');
|
||||||
'HTTP ${response.statusCode}: ${response.reasonPhrase}',
|
|
||||||
);
|
|
||||||
}
|
}
|
||||||
} catch (e) {
|
} catch (e) {
|
||||||
throw Exception('Network error in selectWorkRecordByName: $e');
|
throw Exception('Network error in selectWorkRecordByName: $e');
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user