import 'package:cubit_form/cubit_form.dart'; import 'package:equatable/equatable.dart'; import 'package:selfprivacy/models/courier.dart'; import 'package:selfprivacy/logic/server_api.dart'; export 'package:provider/provider.dart'; part 'couriersstate.dart'; class CouriersCubit extends Cubit { CouriersCubit() : super( const CouriersState( [], false, ), ); final ServerApi api = ServerApi(); Future load() async { refresh(); } Future refresh() async { emit(state.copyWith(isLoading: true)); final resp = await api.getCouriers(); final List items = resp.data; if (items.isNotEmpty) { emit( CouriersState( items, false, ), ); } else { emit(state.copyWith(isLoading: false)); } } Future create(final Courier item) async { await api.addCourier(item); final List items = List.from(state.couriers); items.add(item); emit(state.copyWith(couriers: items)); } @override void clear() async { emit( const CouriersState( [], false, ), ); } }