Bloc observer not showing log
Asked Answered
G

3

10

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

Goltz answered 22/11, 2021 at 18:14 Comment(0)
C
18

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(),
 );
}
Candis answered 22/11, 2021 at 23:4 Comment(2)
Ive tried this solution, but there is still no BloC logs showing. Have it worked for anybody in v8.0.0 ?Burglarize
yes placing runApp() inside of BlocOverrides.runZoned() worked for me. thank you :-)Fortify
R
9

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.
Reardon answered 26/9, 2022 at 8:19 Comment(0)
D
2

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
Devoid answered 15/1, 2022 at 11:41 Comment(1)
@Burglarize try this one, this worked for meDevoid

© 2022 - 2024 — McMap. All rights reserved.