Jisho-Study-Tool/lib/bloc/kanji/kanji_bloc.dart

31 lines
704 B
Dart
Raw Normal View History

2020-07-09 20:06:48 +02:00
import 'dart:async';
2020-07-13 15:18:17 +02:00
import './kanji_event.dart';
import './kanji_state.dart';
2020-07-09 20:06:48 +02:00
2020-07-13 15:18:17 +02:00
import 'package:bloc/bloc.dart';
import 'package:jisho_study_tool/services/kanji_search.dart';
2020-07-09 20:06:48 +02:00
2020-07-13 21:21:33 +02:00
export './kanji_event.dart';
export './kanji_state.dart';
2020-07-09 20:06:48 +02:00
class KanjiBloc extends Bloc<KanjiEvent, KanjiState> {
2020-07-13 15:18:17 +02:00
2020-07-09 20:06:48 +02:00
KanjiBloc() : super(KanjiSearchInitial());
@override
Stream<KanjiState> mapEventToState(
KanjiEvent event,
) async* {
2020-07-13 15:18:17 +02:00
yield KanjiSearchLoading();
if (event is GetKanji) {
try {
final _kanji = await fetchKanji(event.kanjiSearchString);
yield KanjiSearchFinished(_kanji);
} on Exception {
yield KanjiSearchError('Something went wrong');
}
}
2020-07-09 20:06:48 +02:00
}
}