Skip to content

Instantly share code, notes, and snippets.

@GIfatahTH
Created May 28, 2020 18:15
Show Gist options
  • Save GIfatahTH/94023ecc713d69972c660eebcae5d42c to your computer and use it in GitHub Desktop.
Save GIfatahTH/94023ecc713d69972c660eebcae5d42c to your computer and use it in GitHub Desktop.
class CatalogState {
CatalogState({
@required this.repository,
});
//
final CatalogRepository repository;
List<Item> items = [];
Future<CatalogState> getItems() async {
items = await repository.fetchItems();
return this;
}
static Stream<void> deleteItem(
CatalogState currentState,
Item item,
) async* {
final oldItems = [...currentState.items];
try {
currentState.items = oldItems.where((e) => e.id != item.id).toList();
await currentState.repository.removeItem(item);
} catch (e) {
yield currentState.items = oldItems;
rethrow;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment