part of 'grocery_cubit.dart';
sealed class GroceryState extends Equatable {
const GroceryState();
@override
List<Object> get props => [];
}
final class GroceryInitial extends GroceryState {
}
final class GroceryLoading extends GroceryState{}
final class GrocerySuccess extends GroceryState{
final List<GroceryModel> categoryData;
const GrocerySuccess({required this.categoryData});
@override
List<Object> get props => [categoryData];
}
final class GroceryError extends GroceryState{
final String error;
const GroceryError({required this.error});
@override
List<Object> get props => [error];
}
class GroceryModel {
final String id;
final String createdAt;
final String name;
final String image;
final String color;
GroceryModel({
required this.id,
required this.createdAt,
required this.name,
required this.image,
required this.color,
});
factory GroceryModel.fromJson(Map<String, dynamic> json) {
return GroceryModel(
id: json['id'],
createdAt: json['createdAt'],
name: json['name'],
image: json['image'],
color: json['color'],
);
}
}
import 'package:dio/dio.dart';
import 'package:flutter_lessons_in_the_evening/grocery_store/model/grocery_model.dart';
abstract class GroceryRepository {
Future<List<GroceryModel>> getAllCategory();
}
class GroceryRepositoryImpl extends GroceryRepository {
final Dio _dio;
GroceryRepositoryImpl({required Dio dio}) : _dio = dio;
@override
Future<List<GroceryModel>> getAllCategory() async {
try {
final response = await _dio.get(
'https://69987a2cd66520f95f17aeab.mockapi.io/category',
);
if (response.statusCode == 200) {
List<dynamic> data = response.data;
return data
.map((json) => GroceryModel.fromJson(json))
.toList();
} else {
throw Exception('ะพัะธะฑะบะฐ ัะตัะฒะตัะฐ');
}
} on DioException catch (e) {
throw Exception('ะพัะธะฑะบะฐ ัะตัะธ $e');
} catch (e) {
throw Exception('ะฝะตะธะทะฒะตััะฝะฐั ะพัะธะฑะบะฐ $e');
}
}
}