import 'package:chicken_farm/core/api/api_service.dart'; import 'package:chicken_farm/modes/system/dict.dart'; class DictApi { static final DictApi _instance = DictApi._internal(); factory DictApi() => _instance; DictApi._internal(); Future getTypes() async { return await ApiService().get( '/system/dict/type/list', queryParameters: {'pageSize': 10000}, ); } Future getType(String id) async { return await ApiService().get('/system/dict/type/$id'); } // 修改此方法以正确处理返回的字典数据列表 Future?> getDicts(String type) async { try { final response = await ApiService().get('/system/dict/data/type/$type'); if (response != null) { // 根据RuoYi的标准API格式处理响应 if (response is List) { // 直接是列表的情况 return response .map( (item) => item is Map ? DictDataModel.fromJson(item) : DictDataModel( dictCode: 0, dictSort: 0, dictLabel: '', dictValue: '', dictType: type, ), ) .toList(); } else if (response is Map) { // 包含在data字段中的情况 if (response['data'] is List) { return (response['data'] as List) .map( (item) => item is Map ? DictDataModel.fromJson(item) : DictDataModel( dictCode: 0, dictSort: 0, dictLabel: '', dictValue: '', dictType: type, ), ) .toList(); } else if (response['rows'] is List) { // 包含在rows字段中的情况 return (response['rows'] as List) .map( (item) => item is Map ? DictDataModel.fromJson(item) : DictDataModel( dictCode: 0, dictSort: 0, dictLabel: '', dictValue: '', dictType: type, ), ) .toList(); } else { // 单个对象的情况 return [DictDataModel.fromJson(response)]; } } } return null; } catch (e) { // 出现异常时返回null return null; } } Future refreshCache() async { return await ApiService().delete('/system/dict/type/refreshCache'); } }