class MyBloc extends Bloc<MyEvent, MyState> { MyBloc({required Dependency dependency}) : _dependency = dependency, super(const MyState()) { on<MyDataRequested>(_dataRequested); } final Dependency _dependency; FutureOr<void> _dataRequested( MyDataRequested event, Emitter<MyState> emit, ) async { emit(state.copyWith(status: MyStatus.loading)); try { final myAsyncData = await _dependency.asyncCall(); emit( state.copyWith( myAsyncData: myAsyncData, status: MyStatus.success, ), ); } catch (error, stackTrace) { addError(error, stackTrace); emit(state.copyWith(status: MyStatus.error)); } } }
Preview:
downloadDownload PNG
downloadDownload JPEG
downloadDownload SVG
Tip: You can change the style, width & colours of the snippet with the inspect tool before clicking Download!
Click to optimize width for Twitter