не работает поиск :(((

This commit is contained in:
Полина Чубыкина 2024-11-18 13:20:53 +04:00
parent 6a63a40f22
commit 2d28208440
4 changed files with 28 additions and 39 deletions

View File

@ -24,31 +24,20 @@ class PokemonDataDto {
final String? name; final String? name;
final String? url; final String? url;
final String? imageUrl; final String? imageUrl;
final PokemonDetailsDto? details; final int? height; // Добавлено поле для роста
final int? weight; // Добавлено поле для веса
final List<AbilityDto>? abilities; // Добавлено поле для способностей
const PokemonDataDto({ const PokemonDataDto({
this.name, this.name,
this.url, this.url,
this.imageUrl, this.imageUrl,
this.details,
});
factory PokemonDataDto.fromJson(Map<String, dynamic> json) => _$PokemonDataDtoFromJson(json);
}
@JsonSerializable(createToJson: false)
class PokemonDetailsDto {
final int? height;
final int? weight;
final List<AbilityDto>? abilities;
const PokemonDetailsDto({
this.height, this.height,
this.weight, this.weight,
this.abilities, this.abilities,
}); });
factory PokemonDetailsDto.fromJson(Map<String, dynamic> json) => _$PokemonDetailsDtoFromJson(json); factory PokemonDataDto.fromJson(Map<String, dynamic> json) => _$PokemonDataDtoFromJson(json);
} }
@JsonSerializable(createToJson: false) @JsonSerializable(createToJson: false)

View File

@ -20,13 +20,6 @@ PokemonDataDto _$PokemonDataDtoFromJson(Map<String, dynamic> json) =>
name: json['name'] as String?, name: json['name'] as String?,
url: json['url'] as String?, url: json['url'] as String?,
imageUrl: json['imageUrl'] as String?, imageUrl: json['imageUrl'] as String?,
details: json['details'] == null
? null
: PokemonDetailsDto.fromJson(json['details'] as Map<String, dynamic>),
);
PokemonDetailsDto _$PokemonDetailsDtoFromJson(Map<String, dynamic> json) =>
PokemonDetailsDto(
height: (json['height'] as num?)?.toInt(), height: (json['height'] as num?)?.toInt(),
weight: (json['weight'] as num?)?.toInt(), weight: (json['weight'] as num?)?.toInt(),
abilities: (json['abilities'] as List<dynamic>?) abilities: (json['abilities'] as List<dynamic>?)

View File

@ -16,14 +16,7 @@ extension PokemonDataDtoToModel on PokemonDataDto {
CardData toDomain() => CardData( CardData toDomain() => CardData(
name ?? 'UNKNOWN', name ?? 'UNKNOWN',
imageUrl: imageUrl ?? _imagePlaceholder, imageUrl: imageUrl ?? _imagePlaceholder,
descriptionText: details?.toDescriptionText() ?? 'ID: ${url?.split('/')[6] ?? 'UNKNOWN'}', descriptionText: 'Height: ${height ?? 'UNKNOWN'} cm, Weight: ${weight ?? 'UNKNOWN'} g, Abilities: ${abilities?.map((ability) => ability.ability?.name ?? 'UNKNOWN').join(', ') ?? 'UNKNOWN'}',
id: url?.split('/')[6] ?? 'UNKNOWN', id: url?.split('/')[6] ?? 'UNKNOWN',
); );
}
extension PokemonDetailsDtoToModel on PokemonDetailsDto {
String toDescriptionText() {
final abilitiesText = abilities?.map((ability) => ability.ability?.name ?? 'UNKNOWN').join(', ') ?? 'UNKNOWN';
return 'Height: ${height ?? 'UNKNOWN'} cm, Weight: ${weight ?? 'UNKNOWN'} kg, Abilities: $abilitiesText';
}
} }

View File

@ -18,11 +18,11 @@ class PokeRepository extends ApiInterface {
Future<HomeData?> loadData({ Future<HomeData?> loadData({
OnErrorCallback? onError, OnErrorCallback? onError,
String? q, String? q,
int page = 1, int page = 0,
int pageSize = 25, int pageSize = 10,
}) async { }) async {
try { try {
final String url = '$_baseUrl/pokemon'; String url = '$_baseUrl/pokemon';
final Map<String, dynamic> queryParameters = { final Map<String, dynamic> queryParameters = {
'offset': page, 'offset': page,
@ -30,7 +30,7 @@ class PokeRepository extends ApiInterface {
}; };
if (q != null && q.isNotEmpty) { if (q != null && q.isNotEmpty) {
queryParameters['name'] = q; url = '$_baseUrl/pokemon/$q';
} }
final Response<dynamic> response = await _dio.get<Map<dynamic, dynamic>>( final Response<dynamic> response = await _dio.get<Map<dynamic, dynamic>>(
@ -42,15 +42,29 @@ class PokeRepository extends ApiInterface {
final List<PokemonDataDto> pokemonDataList = dto.results ?? []; final List<PokemonDataDto> pokemonDataList = dto.results ?? [];
final List<PokemonDataDto> updatedPokemonDataList = await Future.wait(pokemonDataList.map((pokemonData) async { final List<PokemonDataDto> updatedPokemonDataList = await Future.wait(pokemonDataList.map((pokemonData) async {
final detailsUrl = 'https://pokeapi.co/api/v2/pokemon/${pokemonData.url?.split('/')[6]}'; final id = pokemonData.url?.split('/')[6];
final detailsUrl = 'https://pokeapi.co/api/v2/pokemon/$id';
final detailsResponse = await _dio.get<Map<dynamic, dynamic>>(detailsUrl); final detailsResponse = await _dio.get<Map<dynamic, dynamic>>(detailsUrl);
final PokemonDetailsDto detailsDto = PokemonDetailsDto.fromJson(detailsResponse.data as Map<String, dynamic>);
final String imageUrl = 'https://raw.githubusercontent.com/PokeAPI/sprites/master/sprites/pokemon/${pokemonData.url?.split('/')[6]}.png'; final int height = detailsResponse.data?['height'];
final int weight = detailsResponse.data?['weight'];
final List<dynamic> abilities = detailsResponse.data?['abilities'];
final List<AbilityDto> abilityDtos = abilities.map((ability) {
return AbilityDto(
ability: AbilityDetailDto(
name: ability['ability']['name'],
),
);
}).toList();
return PokemonDataDto( return PokemonDataDto(
name: pokemonData.name, name: pokemonData.name,
url: pokemonData.url, url: pokemonData.url,
imageUrl: imageUrl, imageUrl: 'https://raw.githubusercontent.com/PokeAPI/sprites/master/sprites/pokemon/$id.png',
details: detailsDto, height: height,
weight: weight,
abilities: abilityDtos,
); );
})); }));