pmu/lib/services/api_service.dart

31 lines
769 B
Dart
Raw Normal View History

2024-12-18 10:10:10 +04:00
import 'package:dio/dio.dart';
2024-12-16 22:27:12 +04:00
class ApiService {
2024-12-18 10:10:10 +04:00
final Dio dio;
2024-12-16 22:27:12 +04:00
2024-12-18 10:10:10 +04:00
ApiService({required String baseUrl})
: dio = Dio(BaseOptions(
baseUrl: baseUrl,
connectTimeout: const Duration(seconds: 5),
receiveTimeout: const Duration(seconds: 3),
));
2024-12-16 22:27:12 +04:00
Future<List<dynamic>> fetchHeroes() async {
2024-12-18 10:10:10 +04:00
try {
final response = await dio.get('/v2/heroes');
return response.data as List<dynamic>;
} catch (e) {
throw Exception('Failed to load heroes: $e');
2024-12-16 22:27:12 +04:00
}
}
Future<Map<String, dynamic>> fetchHeroDetails(int id) async {
2024-12-18 10:10:10 +04:00
try {
final response = await dio.get('/v2/heroes/$id');
return response.data as Map<String, dynamic>;
} catch (e) {
throw Exception('Failed to load hero details: $e');
2024-12-16 22:27:12 +04:00
}
}
}