PMU_PIbd32_Kamcharova_K.A/lib/character_service.dart
2024-12-03 16:46:17 +04:00

24 lines
888 B
Dart
Raw Permalink Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

import 'dart:convert';
import 'package:http/http.dart' as http;
import 'character.dart';
const String baseUrl = 'http://192.168.1.83:5000'; // IP-адрес вместо localhost
class CharacterService {
Future<List<Character>> getCharacters({String search = ''}) async {
try {
final response = await http.get(Uri.parse('$baseUrl/characters?search=$search'));
if (response.statusCode == 200) {
final List<dynamic> data = json.decode(response.body);
return data.map((item) => Character.fromJson(item)).toList();
} else {
throw Exception('Ошибка загрузки данных с сервера. Статус: ${response.statusCode}');
}
} catch (e) {
print('Ошибка при получении данных: $e');
throw Exception('Не удалось загрузить персонажей');
}
}
}