0

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);
  }
}

2 Answers 2

1

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);
  }
1
  • @MohamedEl-Alkamy, Glad to help. I did not revise my answer, you can see edit history. please mark this as answer if it helps you. thanks. Mar 26, 2021 at 3:47
0

For flutter_bloc: ^7.0.0 you need to migrate with some changes

v6.1.x

  @override
  void onError(Cubit cubit, Object error, StackTrace stackTrace) {...}

v7.0.0

  @override
  void onError(BlocBase bloc, Object error, StackTrace stackTrace) {...}

Your first positional argument must be BlocBase bloc here.

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.