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

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 { class PokemonDataDto {
final String? name; final String? name;
final String? url; final String? url;
final String? imageUrl;
final PokemonDetailsDto? details;
const PokemonDataDto({ const PokemonDataDto({
this.name, this.name,
this.url, this.url,
this.imageUrl,
this.details,
}); });
factory PokemonDataDto.fromJson(Map<String, dynamic> json) => _$PokemonDataDtoFromJson(json); factory PokemonDataDto.fromJson(Map<String, dynamic> json) => _$PokemonDataDtoFromJson(json);

View File

@ -19,6 +19,10 @@ PokemonDataDto _$PokemonDataDtoFromJson(Map<String, dynamic> json) =>
PokemonDataDto( PokemonDataDto(
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?,
details: json['details'] == null
? null
: PokemonDetailsDto.fromJson(json['details'] as Map<String, dynamic>),
); );
PokemonDetailsDto _$PokemonDetailsDtoFromJson(Map<String, dynamic> json) => PokemonDetailsDto _$PokemonDetailsDtoFromJson(Map<String, dynamic> json) =>

View File

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

View File

@ -17,22 +17,47 @@ class PokeRepository extends ApiInterface {
@override @override
Future<HomeData?> loadData({ Future<HomeData?> loadData({
OnErrorCallback? onError, OnErrorCallback? onError,
String? q,
int page = 1, int page = 1,
int pageSize = 25, int pageSize = 25,
}) async { }) async {
try { try {
final String url = '$_baseUrl/pokemon'; 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>>( final Response<dynamic> response = await _dio.get<Map<dynamic, dynamic>>(
url, url,
queryParameters: { queryParameters: queryParameters,
'offset': (page - 1) * pageSize,
'limit': pageSize,
},
); );
final PokemonDto dto = PokemonDto.fromJson(response.data as Map<String, dynamic>); 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; return data;
} on DioException catch (e) { } on DioException catch (e) {
onError?.call(e.error?.toString()); onError?.call(e.error?.toString());