Jisho-Study-Tool/lib/bloc/search/search_bloc.dart

35 lines
940 B
Dart
Raw Normal View History

2020-07-09 20:06:48 +02:00
import 'dart:async';
import 'package:bloc/bloc.dart';
2020-08-17 11:15:32 +02:00
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
2020-07-09 20:06:48 +02:00
import 'package:meta/meta.dart';
2020-08-19 16:26:25 +02:00
import 'package:jisho_study_tool/services/jisho_search.dart';
import 'package:unofficial_jisho_api/parser.dart';
2020-07-09 20:06:48 +02:00
part 'search_event.dart';
part 'search_state.dart';
class SearchBloc extends Bloc<SearchEvent, SearchState> {
SearchBloc() : super(SearchInitial());
@override
Stream<SearchState> mapEventToState(
SearchEvent event,
) async* {
2020-08-19 16:26:25 +02:00
if (event is GetSearchResults) {
yield SearchLoading();
try {
final _searchResults = await fetchJishoResults(event.searchString);
2020-08-19 18:25:45 +02:00
if (_searchResults.meta.status == 200) yield SearchFinished(_searchResults.data);
2020-08-19 16:26:25 +02:00
} on Exception {
yield SearchError('Something went wrong');
}
} else if (event is ReturnToInitialState) {
2020-08-24 23:46:04 +02:00
yield SearchInitial();
2020-08-19 16:26:25 +02:00
}
2020-07-09 20:06:48 +02:00
}
}