集团观察员不显示日志

Bloc observer not showing log

嘿,我一直将 bloc observer 作为我的 flutter 应用程序中的主要状态管理工具,使用它让事情变得容易多了。 bloc observer 是我用来调试和观察事情发生的主要工具。但是在迁移到 Bloc v8.0.0 后,bloc 观察器已停止记录。

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(),
  );
   ...
}

这是主要功能的片段

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

帮帮我

你的runApp()应该在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(),
 );
}

在我的例子中,除了 blocObserver,我还 HydratedStorage 比我使用的

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

当前集团依赖项:

flutter_bloc: ^8.0.1
hydrated_bloc: ^8.0.0