поменяла апишку
This commit is contained in:
parent
20171525ce
commit
ca93e4ffc0
@ -4,16 +4,12 @@ part 'pokemon_dto.g.dart';
|
||||
|
||||
@JsonSerializable(createToJson: false)
|
||||
class PokemonDto {
|
||||
final List<PokemonDataDto>? results;
|
||||
final int? count;
|
||||
final String? next;
|
||||
final String? previous;
|
||||
final List<PokemonDataDto>? data;
|
||||
final MetaDto? meta;
|
||||
|
||||
const PokemonDto({
|
||||
this.results,
|
||||
this.count,
|
||||
this.next,
|
||||
this.previous,
|
||||
this.data,
|
||||
this.meta,
|
||||
});
|
||||
|
||||
factory PokemonDto.fromJson(Map<String, dynamic> json) => _$PokemonDtoFromJson(json);
|
||||
@ -21,71 +17,48 @@ class PokemonDto {
|
||||
|
||||
@JsonSerializable(createToJson: false)
|
||||
class PokemonDataDto {
|
||||
final String? id;
|
||||
final String? name;
|
||||
final String? url;
|
||||
final String? imageUrl;
|
||||
final int? height;
|
||||
final int? weight;
|
||||
final List<AbilityDto>? abilities;
|
||||
// final PokemonAttributesDataDto? attributes;
|
||||
final List<String>? types;
|
||||
final List<String>? evolvesTo;
|
||||
final PokemonImagesDto? images;
|
||||
|
||||
const PokemonDataDto({
|
||||
this.id,
|
||||
this.name,
|
||||
this.url,
|
||||
this.imageUrl,
|
||||
this.height,
|
||||
this.weight,
|
||||
this.abilities,
|
||||
// this.attributes,
|
||||
this.types,
|
||||
this.evolvesTo,
|
||||
this.images,
|
||||
});
|
||||
|
||||
factory PokemonDataDto.fromJson(Map<String, dynamic> json) => _$PokemonDataDtoFromJson(json);
|
||||
}
|
||||
|
||||
// @JsonSerializable(createToJson: false)
|
||||
// class PokemonAttributesDataDto {
|
||||
// final String? imageUrl;
|
||||
// final int? height;
|
||||
// final int? weight;
|
||||
// final List<AbilityDto>? abilities;
|
||||
// final PokemonSpritesDto? sprites;
|
||||
//
|
||||
// const PokemonAttributesDataDto({
|
||||
// this.imageUrl,
|
||||
// this.height,
|
||||
// this.weight,
|
||||
// this.abilities,
|
||||
// this.sprites,
|
||||
// });
|
||||
//
|
||||
// factory PokemonAttributesDataDto.fromJson(Map<String, dynamic> json) => _$PokemonAttributesDataDtoFromJson(json);
|
||||
// }
|
||||
|
||||
@JsonSerializable(createToJson: false)
|
||||
class AbilityDto {
|
||||
final AbilityDetailDto? ability;
|
||||
class PokemonImagesDto {
|
||||
final String? small;
|
||||
|
||||
const AbilityDto({this.ability});
|
||||
const PokemonImagesDto({this.small});
|
||||
|
||||
factory AbilityDto.fromJson(Map<String, dynamic> json) => _$AbilityDtoFromJson(json);
|
||||
factory PokemonImagesDto.fromJson(Map<String, dynamic> json) => _$PokemonImagesDtoFromJson(json);
|
||||
}
|
||||
|
||||
@JsonSerializable(createToJson: false)
|
||||
class AbilityDetailDto {
|
||||
final String? name;
|
||||
class MetaDto {
|
||||
final PaginationDto? pagination;
|
||||
|
||||
const AbilityDetailDto({this.name});
|
||||
const MetaDto({this.pagination});
|
||||
|
||||
factory AbilityDetailDto.fromJson(Map<String, dynamic> json) => _$AbilityDetailDtoFromJson(json);
|
||||
factory MetaDto.fromJson(Map<String, dynamic> json) => _$MetaDtoFromJson(json);
|
||||
}
|
||||
|
||||
// @JsonSerializable(createToJson: false)
|
||||
// class PokemonSpritesDto {
|
||||
// final String front_default;
|
||||
//
|
||||
// const PokemonSpritesDto({
|
||||
// required this.front_default,
|
||||
// });
|
||||
//
|
||||
// factory PokemonSpritesDto.fromJson(Map<String, dynamic> json) => _$PokemonSpritesDtoFromJson(json);
|
||||
// }
|
||||
@JsonSerializable(createToJson: false)
|
||||
class PaginationDto {
|
||||
final int? current;
|
||||
final int? next;
|
||||
final int? last;
|
||||
|
||||
const PaginationDto({this.current, this.next, this.last});
|
||||
|
||||
factory PaginationDto.fromJson(Map<String, dynamic> json) => _$PaginationDtoFromJson(json);
|
||||
}
|
@ -7,33 +7,42 @@ part of 'pokemon_dto.dart';
|
||||
// **************************************************************************
|
||||
|
||||
PokemonDto _$PokemonDtoFromJson(Map<String, dynamic> json) => PokemonDto(
|
||||
results: (json['results'] as List<dynamic>?)
|
||||
data: (json['data'] as List<dynamic>?)
|
||||
?.map((e) => PokemonDataDto.fromJson(e as Map<String, dynamic>))
|
||||
.toList(),
|
||||
count: (json['count'] as num?)?.toInt(),
|
||||
next: json['next'] as String?,
|
||||
previous: json['previous'] as String?,
|
||||
meta: json['meta'] == null
|
||||
? null
|
||||
: MetaDto.fromJson(json['meta'] as Map<String, dynamic>),
|
||||
);
|
||||
|
||||
PokemonDataDto _$PokemonDataDtoFromJson(Map<String, dynamic> json) =>
|
||||
PokemonDataDto(
|
||||
id: json['id'] as String?,
|
||||
name: json['name'] as String?,
|
||||
url: json['url'] as String?,
|
||||
imageUrl: json['imageUrl'] as String?,
|
||||
height: (json['height'] as num?)?.toInt(),
|
||||
weight: (json['weight'] as num?)?.toInt(),
|
||||
abilities: (json['abilities'] as List<dynamic>?)
|
||||
?.map((e) => AbilityDto.fromJson(e as Map<String, dynamic>))
|
||||
types:
|
||||
(json['types'] as List<dynamic>?)?.map((e) => e as String).toList(),
|
||||
evolvesTo: (json['evolvesTo'] as List<dynamic>?)
|
||||
?.map((e) => e as String)
|
||||
.toList(),
|
||||
);
|
||||
|
||||
AbilityDto _$AbilityDtoFromJson(Map<String, dynamic> json) => AbilityDto(
|
||||
ability: json['ability'] == null
|
||||
images: json['images'] == null
|
||||
? null
|
||||
: AbilityDetailDto.fromJson(json['ability'] as Map<String, dynamic>),
|
||||
: PokemonImagesDto.fromJson(json['images'] as Map<String, dynamic>),
|
||||
);
|
||||
|
||||
AbilityDetailDto _$AbilityDetailDtoFromJson(Map<String, dynamic> json) =>
|
||||
AbilityDetailDto(
|
||||
name: json['name'] as String?,
|
||||
PokemonImagesDto _$PokemonImagesDtoFromJson(Map<String, dynamic> json) =>
|
||||
PokemonImagesDto(
|
||||
small: json['small'] as String?,
|
||||
);
|
||||
|
||||
MetaDto _$MetaDtoFromJson(Map<String, dynamic> json) => MetaDto(
|
||||
pagination: json['pagination'] == null
|
||||
? null
|
||||
: PaginationDto.fromJson(json['pagination'] as Map<String, dynamic>),
|
||||
);
|
||||
|
||||
PaginationDto _$PaginationDtoFromJson(Map<String, dynamic> json) =>
|
||||
PaginationDto(
|
||||
current: (json['current'] as num?)?.toInt(),
|
||||
next: (json['next'] as num?)?.toInt(),
|
||||
last: (json['last'] as num?)?.toInt(),
|
||||
);
|
||||
|
@ -1,24 +1,29 @@
|
||||
import 'package:mobilki_lab1/data/dtos/pokemon_dto.dart';
|
||||
import 'package:mobilki_lab1/domain/models/card.dart';
|
||||
import 'package:mobilki_lab1/domain/models/home.dart';
|
||||
import 'package:flutter/material.dart';
|
||||
|
||||
const _imagePlaceholder =
|
||||
'https://upload.wikimedia.org/wikipedia/en/archive/b/b1/20210811082420%21Portrait_placeholder.png';
|
||||
|
||||
extension PokemonDtoToModel on PokemonDto {
|
||||
HomeData toDomain() => HomeData(
|
||||
data: results?.map((e) => e.toDomain()).toList(),
|
||||
nextPage: next != null ? int.tryParse(next!.split('=')[1].split('&')[0]) : null,
|
||||
data: data?.map((e) => e.toDomain()).toList(),
|
||||
nextPage: meta?.pagination?.next,
|
||||
);
|
||||
}
|
||||
|
||||
extension PokemonDataDtoToModel on PokemonDataDto {
|
||||
CardData toDomain() => CardData(
|
||||
name ?? 'UNKNOWN',
|
||||
imageUrl: imageUrl ?? _imagePlaceholder,
|
||||
descriptionText: 'Height: ${height ?? 'UNKNOWN'} cm, Weight: ${weight ?? 'UNKNOWN'} g, Abilities: ${abilities?.map((ability) => ability.ability?.name ?? 'UNKNOWN').join(', ') ?? 'UNKNOWN'}',
|
||||
// imageUrl: attributes?.sprites?.front_default ?? _imagePlaceholder,
|
||||
// descriptionText: 'Height: ${attributes?.height ?? 'UNKNOWN'} cm, Weight: ${attributes?.weight ?? 'UNKNOWN'} g, Abilities: ${attributes?.abilities?.map((ability) => ability.ability?.name ?? 'UNKNOWN').join(', ') ?? 'UNKNOWN'}',
|
||||
id: url?.split('/')[6] ?? 'UNKNOWN',
|
||||
imageUrl: images?.small ?? _imagePlaceholder,
|
||||
descriptionText: _makeDescriptionText(types, evolvesTo),
|
||||
id: id,
|
||||
);
|
||||
|
||||
String _makeDescriptionText(List<String>? types, List<String>? evolvesTo) {
|
||||
final typeText = types != null && types.isNotEmpty ? 'Types: ${types.join(", ")}' : '';
|
||||
final evolvesToText = evolvesTo != null && evolvesTo.isNotEmpty ? 'Evolves To: ${evolvesTo.join(", ")}' : '';
|
||||
return '$typeText\n$evolvesToText'.trim();
|
||||
}
|
||||
}
|
@ -5,79 +5,36 @@ import 'package:mobilki_lab1/data/repositories/api_interface.dart';
|
||||
import 'package:mobilki_lab1/domain/models/home.dart';
|
||||
import 'package:pretty_dio_logger/pretty_dio_logger.dart';
|
||||
|
||||
class PokeRepository extends ApiInterface {
|
||||
class PokemonRepository extends ApiInterface {
|
||||
static final Dio _dio = Dio()
|
||||
..interceptors.add(PrettyDioLogger(
|
||||
requestHeader: true,
|
||||
requestBody: true,
|
||||
));
|
||||
|
||||
static const String _baseUrl = 'https://pokeapi.co/api/v2';
|
||||
static const String _baseUrl = 'https://api.pokemontcg.io/v2';
|
||||
|
||||
@override
|
||||
Future<HomeData?> loadData({
|
||||
OnErrorCallback? onError,
|
||||
String? q,
|
||||
int page = 0,
|
||||
int pageSize = 5,
|
||||
int page = 1,
|
||||
int pageSize = 10,
|
||||
}) async {
|
||||
try {
|
||||
String url = '$_baseUrl/pokemon';
|
||||
|
||||
Map<String, dynamic> queryParameters = {
|
||||
'offset': page,
|
||||
'limit': pageSize,
|
||||
};
|
||||
|
||||
// if (q != null && q.isNotEmpty) {
|
||||
// url = '$_baseUrl/pokemon/$q';
|
||||
// queryParameters = {
|
||||
// 'offset': page,
|
||||
// 'limit': pageSize,
|
||||
// };
|
||||
// }
|
||||
final String url = '$_baseUrl/cards';
|
||||
|
||||
final Response<dynamic> response = await _dio.get<Map<dynamic, dynamic>>(
|
||||
url,
|
||||
queryParameters: queryParameters,
|
||||
queryParameters: {
|
||||
'q': q,
|
||||
'page': page,
|
||||
'pageSize': pageSize,
|
||||
},
|
||||
);
|
||||
|
||||
// final PokemonDto dto = PokemonDto.fromJson(response.data as Map<String, dynamic>);
|
||||
// final HomeData data = dto.toDomain();
|
||||
final PokemonDto dto = PokemonDto.fromJson(response.data as Map<String, dynamic>);
|
||||
final List<PokemonDataDto> pokemonDataList = dto.results ?? [];
|
||||
|
||||
final List<PokemonDataDto> updatedPokemonDataList = await Future.wait(pokemonDataList.map((pokemonData) async {
|
||||
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 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(
|
||||
name: pokemonData.name,
|
||||
url: pokemonData.url,
|
||||
imageUrl: 'https://raw.githubusercontent.com/PokeAPI/sprites/master/sprites/pokemon/$id.png',
|
||||
height: height,
|
||||
weight: weight,
|
||||
abilities: abilityDtos,
|
||||
);
|
||||
}));
|
||||
|
||||
final HomeData data = HomeData(
|
||||
data: updatedPokemonDataList.map((e) => e.toDomain()).toList(),
|
||||
nextPage: dto.next != null ? int.tryParse(dto.next!.split('=')[1].split('&')[0]) : null,
|
||||
);
|
||||
final HomeData data = dto.toDomain();
|
||||
return data;
|
||||
} on DioException catch (e) {
|
||||
onError?.call(e.error?.toString());
|
||||
|
@ -35,15 +35,15 @@ class MyApp extends StatelessWidget {
|
||||
colorScheme: ColorScheme.fromSeed(seedColor: Colors.deepPurple),
|
||||
useMaterial3: true,
|
||||
),
|
||||
home: RepositoryProvider<PokeRepository>(
|
||||
home: RepositoryProvider<PokemonRepository>(
|
||||
lazy: true,
|
||||
create: (_) => PokeRepository(),
|
||||
create: (_) => PokemonRepository(),
|
||||
child: BlocProvider<LikeBloc>(
|
||||
lazy: false,
|
||||
create: (context) => LikeBloc(),
|
||||
child: BlocProvider<HomeBloc>(
|
||||
lazy: false,
|
||||
create: (context) => HomeBloc(context.read<PokeRepository>()),
|
||||
create: (context) => HomeBloc(context.read<PokemonRepository>()),
|
||||
child: const MyHomePage(title: 'Чубыкина Полина Павловна'),
|
||||
),
|
||||
),
|
||||
|
@ -4,7 +4,7 @@ import 'package:mobilki_lab1/presentation/home_page/bloc/events.dart';
|
||||
import 'package:mobilki_lab1/presentation/home_page/bloc/state.dart';
|
||||
|
||||
class HomeBloc extends Bloc<HomeEvent, HomeState> {
|
||||
final PokeRepository repo;
|
||||
final PokemonRepository repo;
|
||||
|
||||
HomeBloc(this.repo) : super(const HomeState()) {
|
||||
on<HomeLoadDataEvent>(_onLoadData);
|
||||
|
Loading…
Reference in New Issue
Block a user