Skip to content

Instantly share code, notes, and snippets.

@caneto
Last active January 9, 2026 18:46
Show Gist options
  • Select an option

  • Save caneto/64b86a8caeac002c012bdb2ae068d8a3 to your computer and use it in GitHub Desktop.

Select an option

Save caneto/64b86a8caeac002c012bdb2ae068d8a3 to your computer and use it in GitHub Desktop.
GitHub Copilot
// product_res_dm.dart
import 'package:flutter/foundation.dart';
import 'package:json_annotation/json_annotation.dart';
part 'product_res_dm.g.dart';
/// Response model for GET /api/v1/products/{id}
@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 DateTime creationAt;
final DateTime updatedAt;
Map<String, dynamic> toJson() => _$ProductResDmToJson(this);
}
/// Nested model for category in ProductResDm
@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 DateTime creationAt;
final DateTime updatedAt;
Map<String, dynamic> toJson() => _$CategoryDmToJson(this);
}
// 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';
/// Custom API service for Platzi Fake Store API
/// Base URL: https://api.escuelajs.co
@RestApi()
abstract class ProductsApiService {
factory ProductsApiService(Dio dio, {String? baseUrl}) = _ProductsApiService;
/// Get product by ID
@GET('/api/v1/products/{id}')
Future<ProductResDm> getProductById(@Path('id') int id);
}
// products_repository.dart
import 'package:dio/dio.dart';
import '../../../models/api_response/api_result.dart';
import '../../../models/exceptions/network_exception.dart';
import 'models/product_res_dm.dart';
import 'products_api_service.dart';
/// Repository for Platzi Fake Store API products
class ProductsRepository {
ProductsRepository._()
: _apiService = ProductsApiService(
Dio(BaseOptions(baseUrl: 'https://api.escuelajs.co')),
);
static final ProductsRepository instance = ProductsRepository._();
final ProductsApiService _apiService;
/// Get product by ID
Future<ApiResult<ProductResDm>> getProductById(int id) async {
try {
final response = await _apiService.getProductById(id);
return ApiSuccess<ProductResDm>(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