init v2 app
This commit is contained in:
+30
-1
@@ -4,6 +4,7 @@ import '../api/pokemon_api.dart';
|
||||
import '../utils/pokemon_type.dart';
|
||||
import 'package:flutter/material.dart';
|
||||
|
||||
|
||||
// Classe représentant un Pokémon. Elle contient le nom, le numéro et les types du Pokémon.
|
||||
// Elle contient aussi des propriétés calculées pour récupérer l'url de l'image du Pokémon, l'url de l'image shiny du Pokémon et l'url du cri du Pokémon.
|
||||
class Pokemon {
|
||||
@@ -11,6 +12,13 @@ class Pokemon {
|
||||
int id;
|
||||
PokemonType type1;
|
||||
PokemonType? type2;
|
||||
int hp;
|
||||
int atk;
|
||||
int def;
|
||||
int spd;
|
||||
String? description;
|
||||
bool isCaught;
|
||||
bool isSeen;
|
||||
|
||||
String get imageUrl => 'https://raw.githubusercontent.com/Yarkis01/TyraDex/images/sprites/$id/regular.png';
|
||||
String get shinyImageUrl => 'https://raw.githubusercontent.com/Yarkis01/TyraDex/images/sprites/$id/shiny.png';
|
||||
@@ -29,6 +37,13 @@ class Pokemon {
|
||||
required this.id,
|
||||
required this.type1,
|
||||
this.type2, // Le type 2 n'est pas toujours présent
|
||||
required this.hp,
|
||||
required this.atk,
|
||||
required this.def,
|
||||
required this.spd,
|
||||
this.description,
|
||||
this.isCaught = false,
|
||||
this.isSeen = false,
|
||||
});
|
||||
|
||||
// Constructeur qui permet de créer un Pokémon à partir d'un fichier JSON récupéré depuis l'API.
|
||||
@@ -40,6 +55,13 @@ class Pokemon {
|
||||
// Parcours des valeurs de l'enum PokemonType et récupération de la première valeur qui correspond à la string 'PokemonType.${json['type1']}'
|
||||
type1: PokemonType.values.firstWhere((element) => element.toString() == 'PokemonType.${json['type1']}'),
|
||||
type2: json['type2'] != null ? PokemonType.values.firstWhere((element) => element.toString() == 'PokemonType.${json['type2']}') : null,
|
||||
hp: json['hp'] ?? 0,
|
||||
atk: json['atk'] ?? 0,
|
||||
def: json['def'] ?? 0,
|
||||
spd: json['spd'] ?? 0,
|
||||
description: json['description'],
|
||||
isCaught: json['isCaught'] == 1 || json['isCaught'] == true,
|
||||
isSeen: json['isSeen'] == 1 || json['isSeen'] == true,
|
||||
);
|
||||
}
|
||||
|
||||
@@ -50,6 +72,13 @@ class Pokemon {
|
||||
'id': id,
|
||||
'type1': type1.toString().split('.').last, // On récupère la valeur de l'enum PokemonType sans le préfixe 'PokemonType.'
|
||||
'type2': type2?.toString().split('.').last,
|
||||
'hp': hp,
|
||||
'atk': atk,
|
||||
'def': def,
|
||||
'spd': spd,
|
||||
'description': description,
|
||||
'isCaught': isCaught ? 1 : 0,
|
||||
'isSeen': isSeen ? 1 : 0,
|
||||
};
|
||||
}
|
||||
|
||||
@@ -69,7 +98,7 @@ class Pokemon {
|
||||
await PokedexDatabase.insertPokemon(pokemon);
|
||||
}
|
||||
} catch (e) {
|
||||
print(e);
|
||||
debugPrint(e.toString());
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user