Skip to content

Instantly share code, notes, and snippets.

@piyushsinha24
Created September 26, 2019 01:08
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save piyushsinha24/912a9bbc9e0aae8c678d41dac3db6f98 to your computer and use it in GitHub Desktop.
Save piyushsinha24/912a9bbc9e0aae8c678d41dac3db6f98 to your computer and use it in GitHub Desktop.
class PlayerListingBloc extends Bloc<PlayerListingEvent, PlayerListingState> {
final PlayerRepository playerRepository;
PlayerListingBloc({this.playerRepository}) : assert(playerRepository != null);
@override
void onTransition(Transition<PlayerListingEvent, PlayerListingState> transition) {
super.onTransition(transition);
print(transition);
}
@override
PlayerListingState get initialState => PlayerUninitializedState();
@override
Stream<PlayerListingState> mapEventToState(PlayerListingEvent event) async* {
yield PlayerFetchingState();
List<Players> players;
try {
if (event is CountrySelectedEvent) {
players = await playerRepository
.fetchPlayersByCountry(event.nationModel.countryId);
} else if (event is SearchTextChangedEvent) {
players = await playerRepository.fetchPlayersByName(event.searchTerm);
}
if (players.length == 0) {
yield PlayerEmptyState();
} else {
yield PlayerFetchedState(players: players);
}
} catch (_) {
yield PlayerErrorState();
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment