Flutter: simple_bloc_observer.onError

Issue

Anytime I try to run the following code, it fails to compile, the error is in line 17 of the code, it is in the onError string that is right after Future, looking forward to your response.

import 'package:flutter_bloc/flutter_bloc.dart';

class SimpleBlocObserver extends BlocObserver {
  @override
  void onEvent(Bloc bloc, Object event) {
    print(event);
    super.onEvent(bloc, event);
  }

  @override
  void onTransition(Bloc bloc, Transition transition) {
    print(transition);
    super.onTransition(bloc, transition);
  }

  @override
  Future<void> onError(Cubit cubit, Object error, StackTrace stackTrace) async {
    print(error);
    super.onError(cubit, error, stackTrace);
  }
}

Solution

You can change Cubit cubit to BlocBase bloc

code snippet

void onError(BlocBase bloc, Object error, StackTrace stackTrace)

bloc source code

  // Called whenever an [error] is thrown in any [Bloc] or [Cubit].
  /// The [stackTrace] argument may be [StackTrace.empty] if an error
  /// was received without a stack trace.
  @protected
  @mustCallSuper
  void onError(BlocBase bloc, Object error, StackTrace stackTrace) {}

example

code snippet

  @override
  void onError(BlocBase bloc, Object error, StackTrace stackTrace) {
    print('onError -- bloc: ${bloc.runtimeType}, error: $error');
    super.onError(bloc, error, stackTrace);
  }

Answered By – chunhunghan

Answer Checked By – Pedro (FlutterFixes Volunteer)

Leave a Reply

Your email address will not be published. Required fields are marked *