Skip to main content
InterviewHubPRO
Section Overview
Q15

Question 15 of 50

Part 2: Core Architecture & Implementation (Mid-Level)

30%
Flutter & Dart (50 Qs)•Part 2: Core Architecture & Implementation (Mid-Level)

How does the BLoC (Business Logic Component) Pattern work?

senior LevelCode ExampleComponentsSystem Design

In Simple Words: Enforces unidirectional reactive data flow:

  1. UI triggers an Event (e.g. LoginButtonPressed(email, password)).
  2. The BLoC receives the event, executes business logic (API call), and emits new immutable States (LoginLoading -> LoginSuccess).
  3. The UI listens via BlocBuilder or BlocConsumer and re-renders according to the current state.
  • Zero business logic inside UI widgets!
dart
// BLoC Event & State:
class AuthBloc extends Bloc<AuthEvent, AuthState> {
  final AuthRepository repo;
  AuthBloc(this.repo) : super(AuthInitial()) {
    on<LoginSubmitted>((event, emit) async {
      emit(AuthLoading());
      try {
        final user = await repo.login(event.email, event.pass);
        emit(AuthSuccess(user));
      } catch (e) {
        emit(AuthFailure(e.toString()));
      }
    });
  }
}