init v2 app

This commit is contained in:
2026-03-17 13:26:21 +01:00
parent 5f75c53866
commit 528cdcafef
23 changed files with 1169 additions and 153 deletions
+65 -1
View File
@@ -1,13 +1,15 @@
import '../models/pokemon.dart';
import '../utils/pokemon_type.dart';
import 'dart:convert';
import 'package:flutter/material.dart';
import 'package:http/http.dart' as http;
import 'package:flutter/foundation.dart'; // Import for debugPrint
// Classe qui permet de récupérer les données des pokémons depuis l'API Tyradex
// On utilise la librairie http pour effectuer les requêtes
// On utilise la librairie dart:convert pour convertir les données JSON en objet Dart
class PokemonApi {
static const String baseUrl = 'tyradex.vercel.app';
static const String baseUrl = 'tyradex.app';
static const String pokemonUrl = 'api/v1/pokemon';
static Future<Pokemon> getPokemon(int id) async {
@@ -32,12 +34,74 @@ class PokemonApi {
? frenchTypeToEnum(types[1]['name'])
: null;
// Récupération des statistiques
Map<String, dynamic>? stats = json['stats'];
int hp = stats?['hp'] ?? 0;
int atk = stats?['atk'] ?? 0;
int def = stats?['def'] ?? 0;
int spd = stats?['vit'] ?? 0; // 'vit' est la clé pour la vitesse dans tyradex.app
// Récupération de la description
String? description = json['category'];
// On crée un objet Pokemon à partir du fichier JSON
return Pokemon(
name: name,
id: id,
type1: type1,
type2: type2,
hp: hp,
atk: atk,
def: def,
spd: spd,
description: description,
);
}
static Future<List<Pokemon>> getAllPokemon() async {
final response = await http.get(Uri.https(baseUrl, pokemonUrl));
if (response.statusCode == 200) {
List<dynamic> jsonList = jsonDecode(response.body);
List<Pokemon> allPokemon = [];
for (var json in jsonList) {
// Skip default tyradex id 0 response which is generic typing
if(json['pokedex_id'] == 0) continue;
try {
String name = json['name']['fr'];
int id = json['pokedex_id'];
List<dynamic> types = json['types'] ?? [];
PokemonType type1 = frenchTypeToEnum(types[0]['name']);
PokemonType? type2 = types.length > 1 ? frenchTypeToEnum(types[1]['name']) : null;
Map<String, dynamic>? stats = json['stats'];
int hp = stats?['hp'] ?? 0;
int atk = stats?['atk'] ?? 0;
int def = stats?['def'] ?? 0;
int spd = stats?['vit'] ?? 0;
String? description = json['category'];
allPokemon.add(Pokemon(
name: name,
id: id,
type1: type1,
type2: type2,
hp: hp,
atk: atk,
def: def,
spd: spd,
description: description,
));
} catch (e) {
debugPrint("Failed parsing pokemon: ${json['name']} - $e");
}
}
return allPokemon;
} else {
throw Exception('Failed to load pokemon');
}
}
}