import 'package:bloc/bloc.dart'; import 'package:dartz/dartz.dart'; import 'package:freezed_annotation/freezed_annotation.dart'; import 'package:injectable/injectable.dart'; import '../../../domain/wallet/wallet.dart'; part 'outlet_loader_event.dart'; part 'outlet_loader_state.dart'; part 'outlet_loader_bloc.freezed.dart'; @injectable class OutletLoaderBloc extends Bloc { final IOutletRepository _repository; OutletLoaderBloc(this._repository) : super(OutletLoaderState.initial()) { on(_onOutletLoaderEvent); } Future _onOutletLoaderEvent( OutletLoaderEvent event, Emitter emit, ) { return event.map( fetched: (e) async { emit(state.copyWith(isFetching: true, failureOption: none())); final result = await _repository.getOutlets(); final data = result.fold( (f) => state.copyWith(failureOption: optionOf(f)), (outlets) => state.copyWith(outlets: outlets, hasLoaded: true), ); emit(data.copyWith(isFetching: false)); }, cleared: (e) async => emit(OutletLoaderState.initial()), ); } }