course-work/lib/logic/courierscubit.dart

58 lines
1.2 KiB
Dart
Raw Normal View History

2022-12-27 05:19:59 +00:00
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<CouriersState> {
CouriersCubit()
: super(
const CouriersState(
<Courier>[],
false,
),
);
final ServerApi api = ServerApi();
Future<void> load() async {
refresh();
}
Future<void> refresh() async {
emit(state.copyWith(isLoading: true));
final resp = await api.getCouriers();
final List<Courier> items = resp.data;
if (items.isNotEmpty) {
emit(
CouriersState(
items,
false,
),
);
} else {
emit(state.copyWith(isLoading: false));
}
}
Future<void> create(final Courier item) async {
await api.addCourier(item);
final List<Courier> items = List<Courier>.from(state.couriers);
items.add(item);
emit(state.copyWith(couriers: items));
}
@override
void clear() async {
emit(
const CouriersState(
<Courier>[],
false,
),
);
}
}