6 починка пагинации

This commit is contained in:
GokaPek 2024-10-20 12:36:08 +04:00
parent f96bd1b426
commit 9034612747
3 changed files with 17 additions and 42 deletions

View File

@ -8,9 +8,10 @@ part 'foods_dto.g.dart';
@JsonSerializable(createToJson: false)
class FoodsDto {
final List<FoodDataDto>? foods;
final MetaDto? meta;
final int? currentPage;
final int? totalPages;
const FoodsDto({this.foods, this.meta});
const FoodsDto({this.foods, this.currentPage, this.totalPages});
factory FoodsDto.fromJson(Map<String, dynamic> json) => _$FoodsDtoFromJson(json);
@ -26,24 +27,4 @@ class FoodDataDto {
const FoodDataDto({this.fdcId, this.brandName, this.description, this.image});
factory FoodDataDto.fromJson(Map<String, dynamic> json) => _$FoodDataDtoFromJson(json);
}
@JsonSerializable(createToJson: false)
class MetaDto {
final PaginationDto? pagination;
const MetaDto({this.pagination});
factory MetaDto.fromJson(Map<String, dynamic> json) => _$MetaDtoFromJson(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);
}

View File

@ -10,9 +10,8 @@ FoodsDto _$FoodsDtoFromJson(Map<String, dynamic> json) => FoodsDto(
foods: (json['foods'] as List<dynamic>?)
?.map((e) => FoodDataDto.fromJson(e as Map<String, dynamic>))
.toList(),
meta: json['meta'] == null
? null
: MetaDto.fromJson(json['meta'] as Map<String, dynamic>),
currentPage: (json['currentPage'] as num?)?.toInt(),
totalPages: (json['totalPages'] as num?)?.toInt(),
);
FoodDataDto _$FoodDataDtoFromJson(Map<String, dynamic> json) => FoodDataDto(
@ -21,16 +20,3 @@ FoodDataDto _$FoodDataDtoFromJson(Map<String, dynamic> json) => FoodDataDto(
description: json['description'] as String?,
image: json['image'] 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(),
);

View File

@ -12,8 +12,16 @@ extension CharacterDataDtoToModel on FoodDataDto {
}
extension CharactersDtoToModel on FoodsDto {
HomeData toDomain() => HomeData(
data: foods?.map((e) => e.toDomain()).toList(),
nextPage: meta?.pagination?.next,
);
HomeData toDomain() {
int? nextPage = currentPage;
if (currentPage != null && totalPages != null && totalPages! > currentPage!) {
nextPage = currentPage! + 1;
}
return HomeData(
data: foods?.map((e) => e.toDomain()).toList(),
nextPage: nextPage,
);
}
}