Skip to content

Instantly share code, notes, and snippets.

@techpotatoes
Last active April 18, 2020 14:36
Show Gist options
  • Save techpotatoes/23080002b3b5a5f28b05301e2cbe5caf to your computer and use it in GitHub Desktop.
Save techpotatoes/23080002b3b5a5f28b05301e2cbe5caf to your computer and use it in GitHub Desktop.
Flutter development series - part4
class LoyaltyBloc extends Bloc<LoyaltyEvent, LoyaltyState>{
final LoyaltyCardRepository loyaltyCardRepository;
LoyaltyBloc({@required this.loyaltyCardRepository});
@override
LoyaltyState get initialState => LoyaltyEmpty();
@override
Stream<LoyaltyState> mapEventToState(LoyaltyEvent event) async* {
switch (event.runtimeType) {
case Fetch:
try {
yield LoyaltyLoading();
final _loyaltyCards = await _fetchLoyaltyCards();
if (_loyaltyCards.length > 0) {
yield LoyaltyLoaded(loyaltyCards: _loyaltyCards);
}else {
yield LoyaltyEmpty();
}
} catch(_) {
yield LoyaltyError();
}
break;
default:
yield LoyaltyError();
}
}
Future<List<LoyaltyCard>> _fetchLoyaltyCards() async {
return loyaltyCardRepository.getAll();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment