Fix Models and Json Parsing

This commit is contained in:
nikbel2004@outlook.com 2024-12-20 01:21:34 +04:00
parent f866f2bec2
commit a05311d3e1
10 changed files with 38 additions and 33 deletions

View File

@ -22,10 +22,15 @@ class CandiesResponseDto {
@JsonSerializable(createToJson: false)
class CandyItemDto {
final int? beanId;
final List<String>? groupName;
@JsonKey(name: "beanId")
final int? candyId;
@JsonKey(name: "groupName")
final List<String>? manufacturer;
final List<String>? ingredients;
final String? flavorName;
@JsonKey(name: "flavorName")
final String? name;
final String? description;
final String? colorGroup;
final String? backgroundColor;
@ -36,10 +41,10 @@ class CandyItemDto {
final bool? kosher;
const CandyItemDto(
{this.beanId,
this.groupName,
{this.candyId,
this.manufacturer,
this.ingredients,
this.flavorName,
this.name,
this.description,
this.colorGroup,
this.backgroundColor,

View File

@ -1,19 +1,19 @@
import 'package:candystore/data/dtos/candies_dto.dart';
import 'package:candystore/models/card_data.dart';
import 'package:candystore/models/candy_data.dart';
import 'package:candystore/models/home_data.dart';
extension CandyItemDtoToModel on CandyItemDto {
CardData toDomain() => CardData(
flavorName: flavorName ?? "UNKNOWN",
name: name ?? "UNKNOWN",
imageUrl: imageUrl ??
"https://upload.wikimedia.org/wikipedia/commons/a/a2/Person_Image_Placeholder.png",
description: description ?? "UNKNOWN",
groupName: groupName ?? ["UNKNOWN"],
beanId: beanId);
manufacturer: manufacturer ?? ["UNKNOWN"],
candyId: candyId);
}
extension CandiesDtoToModel on CandiesResponseDto {
CandyData toDomain() => CandyData(
HomeData toDomain() => HomeData(
data: items?.map((e) => e.toDomain()).toList(),
nextPage: currentPage != null && totalPages != null && currentPage! < totalPages!
? currentPage! + 1

View File

@ -1,7 +1,7 @@
import 'package:candystore/models/candy_data.dart';
import 'package:candystore/models/home_data.dart';
typedef OnErrorCallback = void Function(String? error);
abstract class ApiInterface {
Future<CandyData?> loadData({OnErrorCallback? onError});
Future<HomeData?> loadData({OnErrorCallback? onError});
}

View File

@ -5,7 +5,7 @@ import 'package:pretty_dio_logger/pretty_dio_logger.dart';
import 'package:candystore/data/dtos/candies_dto.dart';
import 'package:candystore/data/mapper/candies_mapper.dart';
import 'package:candystore/data/repositories/api_interface.dart';
import 'package:candystore/models/candy_data.dart';
import 'package:candystore/models/home_data.dart';
class CandyRepository extends ApiInterface {
static final Dio _dio = Dio(BaseOptions(connectTimeout: const Duration(seconds: 10)))
@ -14,7 +14,7 @@ class CandyRepository extends ApiInterface {
static const String _baseUrl = "https://jellybellywikiapi.onrender.com/api";
@override
Future<CandyData?> loadData(
Future<HomeData?> loadData(
{OnErrorCallback? onError, String? q, int page = 1, int pageSize = 10}) async {
try {
const String url = '$_baseUrl/Beans?';
@ -34,7 +34,7 @@ class CandyRepository extends ApiInterface {
if (q != null && q.isNotEmpty) {
final query = q.toLowerCase();
filteredItems = dto.items?.where((item) {
final flavorName = item.flavorName?.toLowerCase() ?? '';
final flavorName = item.name?.toLowerCase() ?? '';
return flavorName.contains(query); // Динамическое совпадение
}).toList();
}

View File

@ -1,14 +1,14 @@
class CardData {
final String flavorName;
final String name;
final String imageUrl;
final String description;
final List<String> groupName;
final int? beanId;
final List<String> manufacturer;
final int? candyId;
const CardData(
{required this.flavorName,
{required this.name,
required this.imageUrl,
required this.description,
required this.groupName,
required this.beanId});
required this.manufacturer,
required this.candyId});
}

View File

@ -1,10 +1,10 @@
import 'package:candystore/models/card_data.dart';
class CandyData {
class HomeData {
final List<CardData>? data;
final int? nextPage;
final int? currentPage;
final int? totalPages;
CandyData({this.data, this.nextPage, this.currentPage, this.totalPages});
HomeData({this.data, this.nextPage, this.currentPage, this.totalPages});
}

View File

@ -1,8 +1,8 @@
import 'package:candystore/models/candy_data.dart';
import 'package:candystore/models/home_data.dart';
import 'package:equatable/equatable.dart';
class CandyState extends Equatable {
final CandyData? data;
final HomeData? data;
final bool isLoading;
final bool isPaginationLoading;
final String? error;
@ -12,7 +12,7 @@ class CandyState extends Equatable {
// Получение нового экземпляра состояния
CandyState copyWith(
{CandyData? data, bool? isLoading, bool? isPaginationLoading, String? error}) =>
{HomeData? data, bool? isLoading, bool? isPaginationLoading, String? error}) =>
CandyState(
data: data ?? this.data,
isLoading: isLoading ?? this.isLoading,

View File

@ -10,7 +10,7 @@ class DetailsPage extends StatelessWidget {
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(title: Text('Sweet ${data.flavorName}')),
appBar: AppBar(title: Text('Sweet ${data.name}')),
body: SingleChildScrollView(
padding: const EdgeInsets.all(30),
child: Column(
@ -27,11 +27,11 @@ class DetailsPage extends StatelessWidget {
),
),
const SizedBox(height: 20),
Text("${context.locale.titleCard}: ${data.flavorName}",
Text("${context.locale.titleCard}: ${data.name}",
style: const TextStyle(fontSize: 20, color: Colors.blue)),
Text("${context.locale.descriptionCard}: ${data.description}",
style: const TextStyle(fontSize: 16, color: Colors.red)),
Text("${context.locale.manufacturer}: ${data.groupName}",
Text("${context.locale.manufacturer}: ${data.manufacturer}",
style: const TextStyle(fontSize: 16, color: Colors.orangeAccent)),
],
),

View File

@ -167,7 +167,7 @@ class _MyCandyPageState extends State<MyCandyPage> {
: Card.fromData(
data,
isLiked:
likeState.likedIds?.contains(data.beanId) == true,
likeState.likedIds?.contains(data.candyId) == true,
onLike: (int? id, String text, bool isLiked) =>
_onLike(id, text, isLiked),
onTap: () => _navigateToDetailsPage(context, data),

View File

@ -27,8 +27,8 @@ class Card extends StatelessWidget {
VoidCallback? onTap,
bool isLiked = false}) =>
Card(
id: data.beanId,
name: data.flavorName,
id: data.candyId,
name: data.name,
description: data.description,
image: data.imageUrl,
isLiked: isLiked,