1
0
mirror of https://github.com/h7x4/Jisho-Study-Tool.git synced 2024-12-22 13:57:29 +01:00
Jisho-Study-Tool/lib/bloc/search/search_bloc.dart

35 lines
940 B
Dart

import 'dart:async';
import 'package:bloc/bloc.dart';
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:meta/meta.dart';
import 'package:jisho_study_tool/services/jisho_search.dart';
import 'package:unofficial_jisho_api/parser.dart';
part 'search_event.dart';
part 'search_state.dart';
class SearchBloc extends Bloc<SearchEvent, SearchState> {
SearchBloc() : super(SearchInitial());
@override
Stream<SearchState> mapEventToState(
SearchEvent event,
) async* {
if (event is GetSearchResults) {
yield SearchLoading();
try {
final _searchResults = await fetchJishoResults(event.searchString);
if (_searchResults.meta.status == 200) yield SearchFinished(_searchResults.data);
} on Exception {
yield SearchError('Something went wrong');
}
} else if (event is ReturnToInitialState) {
yield SearchInitial();
}
}
}