я сделала все что могла

This commit is contained in:
Полина Чубыкина 2024-11-17 21:35:47 +04:00
parent 9f1b790c86
commit 6a63a40f22
4 changed files with 44 additions and 16 deletions

View File

@ -23,10 +23,14 @@ class PokemonDto {
class PokemonDataDto {
final String? name;
final String? url;
final String? imageUrl;
final PokemonDetailsDto? details;
const PokemonDataDto({
this.name,
this.url,
this.imageUrl,
this.details,
});
factory PokemonDataDto.fromJson(Map<String, dynamic> json) => _$PokemonDataDtoFromJson(json);

View File

@ -19,6 +19,10 @@ PokemonDataDto _$PokemonDataDtoFromJson(Map<String, dynamic> json) =>
PokemonDataDto(
name: json['name'] as String?,
url: json['url'] 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) =>

View File

@ -13,17 +13,12 @@ extension PokemonDtoToModel on PokemonDto {
}
extension PokemonDataDtoToModel on PokemonDataDto {
CardData toDomain() {
final id = url?.split('/')[6] ?? 'UNKNOWN';
final imageUrl = 'https://raw.githubusercontent.com/PokeAPI/sprites/master/sprites/pokemon/$id.png';
return CardData(
CardData toDomain() => CardData(
name ?? 'UNKNOWN',
imageUrl: imageUrl,
descriptionText: 'ID: $id',
id: id,
imageUrl: imageUrl ?? _imagePlaceholder,
descriptionText: details?.toDescriptionText() ?? 'ID: ${url?.split('/')[6] ?? 'UNKNOWN'}',
id: url?.split('/')[6] ?? 'UNKNOWN',
);
}
}
extension PokemonDetailsDtoToModel on PokemonDetailsDto {

View File

@ -17,22 +17,47 @@ class PokeRepository extends ApiInterface {
@override
Future<HomeData?> loadData({
OnErrorCallback? onError,
String? q,
int page = 1,
int pageSize = 25,
}) async {
try {
final String url = '$_baseUrl/pokemon';
final Map<String, dynamic> queryParameters = {
'offset': page,
'limit': pageSize,
};
if (q != null && q.isNotEmpty) {
queryParameters['name'] = q;
}
final Response<dynamic> response = await _dio.get<Map<dynamic, dynamic>>(
url,
queryParameters: {
'offset': (page - 1) * pageSize,
'limit': pageSize,
},
queryParameters: queryParameters,
);
final PokemonDto dto = PokemonDto.fromJson(response.data as Map<String, dynamic>);
final HomeData data = dto.toDomain();
final List<PokemonDataDto> pokemonDataList = dto.results ?? [];
final List<PokemonDataDto> updatedPokemonDataList = await Future.wait(pokemonDataList.map((pokemonData) async {
final detailsUrl = 'https://pokeapi.co/api/v2/pokemon/${pokemonData.url?.split('/')[6]}';
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';
return PokemonDataDto(
name: pokemonData.name,
url: pokemonData.url,
imageUrl: imageUrl,
details: detailsDto,
);
}));
final HomeData data = HomeData(
data: updatedPokemonDataList.map((e) => e.toDomain()).toList(),
nextPage: dto.next != null ? int.tryParse(dto.next!.split('=')[1].split('&')[0]) : null,
);
return data;
} on DioException catch (e) {
onError?.call(e.error?.toString());