Home > Blockchain >  Bloc observer not showing log
Bloc observer not showing log

Time:11-23

Hey I've been bloc observer as the main state management tool in my flutter app and using it made things much easier. The bloc observer is the main tool I use to debug and observe things happening. But after migrating to the Bloc v8.0.0 bloc observer has stopped logging.

void main() async {
  WidgetsFlutterBinding.ensureInitialized();
  HttpOverrides.global = MyHttpOverrides();
  await Hive.initFlutter();
  Hive.registerAdapter(UserAdapter());
  await Hive.openBox<User>('user');
  await Firebase.initializeApp();
  BlocOverrides.runZoned(
    () {},
    blocObserver: SimpleBlocObserver(),
  );
   ...
}

This is snippet of the main function

Bloc observer

import 'package:flutter_bloc/flutter_bloc.dart';

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

  @override
  void onChange(BlocBase bloc, Change change) {
    super.onChange(bloc, change);
    print(change);
  }

  @override
  void onCreate(BlocBase bloc) {
    super.onCreate(bloc);
    print(bloc);
  }

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

  @override
  void one rror(BlocBase bloc, Object error, StackTrace stackTrace) {
    print(error);
    super.onError(bloc, error, stackTrace);
  }
}

Help me out

CodePudding user response:

Your runApp() should be inside BlocOverrides.runZoned()

void main() async {
 WidgetsFlutterBinding.ensureInitialized();
 HttpOverrides.global = MyHttpOverrides();
 await Hive.initFlutter();
 Hive.registerAdapter(UserAdapter());
 await Hive.openBox<User>('user');
 await Firebase.initializeApp();
 BlocOverrides.runZoned(
   () {
     runApp(App())
   },
   blocObserver: SimpleBlocObserver(),
 );
}
  • Related