Skip to content

Instantly share code, notes, and snippets.

@red-star25
Created January 6, 2022 16:38
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save red-star25/d6a53417ac5b9fc1ef4f4cad742feeed to your computer and use it in GitHub Desktop.
Save red-star25/d6a53417ac5b9fc1ef4f4cad742feeed to your computer and use it in GitHub Desktop.
import 'package:bloc/bloc.dart';
import 'package:bloc_api_example/data/model/joke_model.dart';
import 'package:bloc_api_example/data/repositories/joke_repository.dart';
import 'package:equatable/equatable.dart';
import 'package:meta/meta.dart';
part 'joke_event.dart';
part 'joke_state.dart';
class JokeBloc extends Bloc<JokeEvent, JokeState> {
final JokeRepository _jokeRepository;
JokeBloc(this._jokeRepository) : super(JokeLoadingState()) {
on<LoadJokeEvent>((event, emit) async {
emit(JokeLoadingState());
try {
final joke = await _jokeRepository.getJoke();
emit(JokeLoadedState(joke));
} catch (e) {
emit(JokeErrorState(e.toString()));
}
});
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment