import 'package:dio/dio.dart'; import 'package:flutter_riverpod/flutter_riverpod.dart'; import '../core/network/api_exception.dart'; import '../core/network/dio_client.dart'; import '../models/category.dart'; final categoryApiProvider = Provider((ref) => CategoryApi(ref.watch(dioProvider))); class CategoryApi { final Dio _dio; CategoryApi(this._dio); /// 获取分类列表,type: 1=支出 2=收入(后端可能返回裸数组或 {code,data}) Future> getCategories(int type) async { try { final res = await _dio.get('/categories', queryParameters: {'type': type}); return _parseList(res.data) .whereType>() .map(Category.fromJson) .toList(); } on DioException catch (e) { final err = e.error; throw err is ApiException ? err : ApiException(-1, '请求失败'); } } List _parseList(Object? data) { if (data is List) return data; if (data is Map && data['data'] is List) return data['data'] as List; throw ApiException(-1, '分类响应格式异常'); } }