PMU/lib/card_repository.dart

20 lines
588 B
Dart
Raw Normal View History

2024-10-08 23:25:53 +04:00
import 'dart:convert';
import 'package:http/http.dart' as http;
import 'card_model.dart';
class CardRepository {
Future<List<CardModel>> fetchCards() async {
final response = await http.get(Uri.parse('https://deckofcardsapi.com/api/deck/new/draw/?count=5'));
if (response.statusCode == 200) {
final data = json.decode(response.body);
List<CardModel> cards = (data['cards'] as List)
.map((card) => CardModel.fromJson(card))
.toList();
return cards;
} else {
throw Exception('Ошибка загрузки карт');
}
}
}