27 lines
708 B
Dart
27 lines
708 B
Dart
|
import 'package:flutter_bloc/flutter_bloc.dart';
|
||
|
import 'package:pmu_new/presentation/home_page/bloc/state.dart';
|
||
|
import '../../../data/repositories/PlacesRepository.dart';
|
||
|
import 'events.dart';
|
||
|
|
||
|
class HomeBloc extends Bloc<HomeEvent, HomeState> {
|
||
|
final PlacesRepository repo;
|
||
|
String? error;
|
||
|
|
||
|
HomeBloc(this.repo) : super(const HomeState()) {
|
||
|
on<HomeLoadDataEvent>(_onLoadData);
|
||
|
}
|
||
|
|
||
|
Future<void> _onLoadData(HomeLoadDataEvent event, Emitter<HomeState> emit) async {
|
||
|
emit(state.copyWith(isLoading: true));
|
||
|
|
||
|
final data = await repo.loadData(q: event.search, onError: (e) => error = e,);
|
||
|
|
||
|
|
||
|
emit(state.copyWith(
|
||
|
isLoading: false,
|
||
|
data: data,
|
||
|
error: error,
|
||
|
));
|
||
|
}
|
||
|
}
|