import 'package:cubit_form/cubit_form.dart'; import 'package:equatable/equatable.dart'; import 'package:selfprivacy/logic/server_api.dart'; import 'package:selfprivacy/models/order.dart'; export 'package:provider/provider.dart'; part 'ordersstate.dart'; class OrdersCubit extends Cubit { OrdersCubit() : super( const OrdersState( [], false, ), ); final ServerApi api = ServerApi(); Future load() async { refresh(); } Future refresh() async { emit(state.copyWith(isLoading: true)); final resp = await api.getOrders(); final List items = resp.data; if (items.isNotEmpty) { emit( OrdersState( items, false, ), ); } else { emit(state.copyWith(isLoading: false)); } } Future create(final Order order) async { await api.createOrder(order); final List items = List.from(state.orders); items.add(order); emit(state.copyWith(orders: items)); } @override void clear() async { emit( const OrdersState( [], false, ), ); } }