Created
January 9, 2026 18:39
-
-
Save caneto/4ad94dd8db672b52aa57b3c8f3429fb6 to your computer and use it in GitHub Desktop.
Codigo Antigravity
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| // product_res_dm.dart | |
| import 'package:flutter/foundation.dart'; | |
| import 'package:json_annotation/json_annotation.dart'; | |
| part 'product_res_dm.g.dart'; | |
| @JsonSerializable() | |
| @immutable | |
| class ProductResDm { | |
| const ProductResDm({ | |
| required this.id, | |
| required this.title, | |
| required this.slug, | |
| required this.price, | |
| required this.description, | |
| required this.category, | |
| required this.images, | |
| required this.creationAt, | |
| required this.updatedAt, | |
| }); | |
| factory ProductResDm.fromJson(Map<String, dynamic> json) => | |
| _$ProductResDmFromJson(json); | |
| final int id; | |
| final String title; | |
| final String slug; | |
| final int price; | |
| final String description; | |
| final CategoryDm category; | |
| final List<String> images; | |
| final String creationAt; | |
| final String updatedAt; | |
| Map<String, dynamic> toJson() => _$ProductResDmToJson(this); | |
| } | |
| @JsonSerializable() | |
| @immutable | |
| class CategoryDm { | |
| const CategoryDm({ | |
| required this.id, | |
| required this.name, | |
| required this.slug, | |
| required this.image, | |
| required this.creationAt, | |
| required this.updatedAt, | |
| }); | |
| factory CategoryDm.fromJson(Map<String, dynamic> json) => | |
| _$CategoryDmFromJson(json); | |
| final int id; | |
| final String name; | |
| final String slug; | |
| final String image; | |
| final String creationAt; | |
| final String updatedAt; | |
| Map<String, dynamic> toJson() => _$CategoryDmToJson(this); | |
| } | |
| // products_constants.dart | |
| class ProductsConstants { | |
| static const String baseUrl = 'https://api.escuelajs.co'; | |
| } | |
| // products_api_service.dart | |
| import 'package:dio/dio.dart'; | |
| import 'package:retrofit/retrofit.dart'; | |
| import 'models/product_res_dm.dart'; | |
| part 'products_api_service.g.dart'; | |
| @RestApi() | |
| abstract class ProductsApiService { | |
| factory ProductsApiService(Dio dio) = _ProductsApiService; | |
| @GET('/api/v1/products/{id}') | |
| Future<ProductResDm> getProductById(@Path('id') int id); | |
| } | |
| // products_repository.dart | |
| import 'package:dio/dio.dart'; | |
| import '../../../values/constants/products_constants.dart'; | |
| import '../../../models/api_response/api_result.dart'; | |
| import '../../../models/exceptions/network_exception.dart'; | |
| import '../api_repository/api_repository.dart'; | |
| import 'products_api_service.dart'; | |
| import 'models/product_res_dm.dart'; | |
| class ProductsRepository extends Repository { | |
| factory ProductsRepository() => _instance; | |
| ProductsRepository._() : super.withDio(dio: Dio()); | |
| static final _apiService = ProductsApiService( | |
| Dio(BaseOptions(baseUrl: ProductsConstants.baseUrl)), | |
| ); | |
| static final _instance = ProductsRepository._(); | |
| /// Get product by ID | |
| Future<ApiResult<ProductResDm>> getProductById(int id) async { | |
| try { | |
| final response = await _apiService.getProductById(id); | |
| return ApiSuccess(data: response); | |
| } catch (e, s) { | |
| return ApiFailure(NetworkException.fromError(e, s)); | |
| } | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment