import 'package:cubit_form/cubit_form.dart'; import 'package:equatable/equatable.dart'; import 'package:selfprivacy/models/item.dart'; import 'package:selfprivacy/logic/server_api.dart'; export 'package:provider/provider.dart'; part 'itemsstate.dart'; class ItemsCubit extends Cubit { ItemsCubit() : super( const ItemsState( [], false, ), ); final ServerApi api = ServerApi(); Future load() async { refresh(); } Future refresh() async { emit(state.copyWith(isLoading: true)); final resp = await api.getItems(); final List items = resp.data; if (items.isNotEmpty) { emit( ItemsState( items, false, ), ); } else { emit(state.copyWith(isLoading: false)); } } Future create(final Item item) async { await api.createItem(item); final List items = List.from(state.items); items.add(item); emit(state.copyWith(items: items)); } Future delete(final Item item) async { final List items = List.from(state.items); await api.deleteItem(item); items.removeWhere((final Item u) => u.id == item.id); emit(state.copyWith(items: items)); } @override void clear() async { emit( const ItemsState( [], false, ), ); } }