36 lines
1.1 KiB
Dart
36 lines
1.1 KiB
Dart
import 'dart:convert';
|
|
import 'package:http/http.dart' as http;
|
|
|
|
class ApiService {
|
|
final String baseUrl;
|
|
|
|
ApiService({required this.baseUrl});
|
|
|
|
Future<List<dynamic>> fetchHeroes() async {
|
|
final response = await http.get(Uri.parse('$baseUrl/v2/heroes'));
|
|
if (response.statusCode == 200) {
|
|
return json.decode(response.body) as List<dynamic>;
|
|
} else {
|
|
throw Exception('Failed to load heroes');
|
|
}
|
|
}
|
|
|
|
Future<Map<String, dynamic>> fetchHeroDetails(int id) async {
|
|
final response = await http.get(Uri.parse('$baseUrl/v2/heroes/$id'));
|
|
if (response.statusCode == 200) {
|
|
return json.decode(response.body) as Map<String, dynamic>;
|
|
} else {
|
|
throw Exception('Failed to load hero details');
|
|
}
|
|
}
|
|
|
|
Future<Map<String, dynamic>> searchHeroByName(String name) async {
|
|
final response = await http.get(Uri.parse('$baseUrl/v2/heroes/by-name/$name'));
|
|
if (response.statusCode == 200) {
|
|
return json.decode(response.body) as Map<String, dynamic>;
|
|
} else {
|
|
throw Exception('Failed to search hero');
|
|
}
|
|
}
|
|
}
|