Last active
April 18, 2020 14:36
-
-
Save techpotatoes/23080002b3b5a5f28b05301e2cbe5caf to your computer and use it in GitHub Desktop.
Flutter development series - part4
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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