7

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 onError(BlocBase bloc, Object error, StackTrace stackTrace) {
    print(error);
    super.onError(bloc, error, stackTrace);
  }
}

Help me out

biniyam112
  • 956
  • 1
  • 11
  • 17

3 Answers3

17

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(),
 );
}
rapaterno
  • 626
  • 4
  • 6
  • 1
    Ive tried this solution, but there is still no BloC logs showing. Have it worked for anybody in v8.0.0 ? – kashlo Dec 14 '21 at 20:08
  • 1
    yes placing runApp() inside of BlocOverrides.runZoned() worked for me. thank you :-) – Adamski Jan 24 '22 at 16:00
7

Now BlocOverrides.runZoned is deprecated. Below example works for me.

    void main() async {
      WidgetsFlutterBinding.ensureInitialized();
      Bloc.observer = AppObserver();
      await initBlocsAndDependencies();
      runApp(const App());
    }
  • Note: BlocObserver should be initialized before creating blocs.
MohsenZ
  • 276
  • 4
  • 8
1

In my case besides blocObserver I had also HydratedStorage than I used

void main() async {
  WidgetsFlutterBinding.ensureInitialized();

  final storage = await HydratedStorage.build(
    storageDirectory: await getApplicationDocumentsDirectory(),
  );

  HydratedBlocOverrides.runZoned(
    () => runApp(MyApp(
      appRouter: AppRouter(),
      connectivity: Connectivity(),
    )),
    storage: storage,
    blocObserver: AppBlocObserver(),
  );
}

current bloc dependencies:

flutter_bloc: ^8.0.1
hydrated_bloc: ^8.0.0