bloc observer

PHOTO EMBED

Thu Jul 21 2022 09:37:04 GMT+0000 (UTC)

Saved by @Samuel1347 #flutter #dart

import 'package:flutter/foundation.dart';
import 'package:flutter_bloc/flutter_bloc.dart';

class BlocLoggerObserver extends BlocObserver {
  @override
  void onCreate(BlocBase bloc) {
    super.onCreate(bloc);
    _log("bloc created: $bloc");
  }

  @override
  void onClose(BlocBase bloc) {
    super.onClose(bloc);
    _log("bloc closed: $bloc");
  }

  @override
  void onChange(BlocBase bloc, Change change) {
    super.onChange(bloc, change);
    _log("bloc changed: $bloc, $change");
  }

  @override
  void onError(BlocBase bloc, Object error, StackTrace stackTrace) {
    super.onError(bloc, error, stackTrace);
    _log("bloc error: $bloc, $error, $stackTrace");
  }

  @override
  void onEvent(Bloc bloc, Object? event) {
    super.onEvent(bloc, event);
    _log("bloc event: $bloc, $event");
  }


  void _log(message) {
    if (kDebugMode) {
      print(message);
    }
  }
}
content_copyCOPY

sample import 'package:firebase_core/firebase_core.dart'; import 'package:flutter/material.dart'; import 'package:flutter_bloc/flutter_bloc.dart'; import 'package:novelapp/application/bloc/edit_profile/edit_profile_cubit.dart'; import 'package:novelapp/application/bloc/quiz/quiz_cubit.dart'; import 'package:novelapp/data/services/quez_service.dart'; import 'package:novelapp/screens/connect/bloc/chats/messaging_cubit.dart'; import 'package:novelapp/screens/login/bloc/login_bloc.dart'; import 'package:novelapp/screens/promt/bloc/prompt/prompt_cubit.dart'; import 'package:provider/provider.dart'; import 'application/application.dart'; import 'application/bloc/authentication/authentication_bloc.dart'; import 'application/bloc_logger_observer.dart'; import 'data/services/network_service.dart'; import 'data/services/user_service.dart'; import 'domain/repositories/secure_storage.dart'; import 'firebase_options.dart'; import 'providers/bottom_sheet_provider.dart'; import 'screens/sign_up_screen/bloc/sign_up_cubit.dart'; void main() async { WidgetsFlutterBinding.ensureInitialized(); await Firebase.initializeApp( options: DefaultFirebaseOptions.currentPlatform, ); final secureStorage = SecureStorage(); final blocLogger = BlocLoggerObserver(); await NetworkService.instance.initClient(); final UserService userService = UserService(NetworkService.instance); final QuizService quizService = QuizService(NetworkService.instance); BlocOverrides.runZoned( () { final authBloc = AuthenticationBloc(secureStorage, userService); final loginBloc = LoginBloc( authenticationBloc: authBloc, userService: userService, ); final signUpCubit = SignUpCubit( userService: userService, authenticationBloc: authBloc, ); final promptCubit = PromptCubit(quizService); final editProfile = EditProfileCubit(userService); final quizCubit = QuizCubit(quizService); final chatCubit = MessagingCubit(userService); runApp( MultiBlocProvider( providers: [ BlocProvider<AuthenticationBloc>(create: (context) => authBloc), BlocProvider<LoginBloc>(create: (context) => loginBloc), BlocProvider<SignUpCubit>(create: (context) => signUpCubit), BlocProvider<PromptCubit>(create: (context) => promptCubit), BlocProvider<EditProfileCubit>(create: (context) => editProfile), BlocProvider<QuizCubit>(create: (context) => quizCubit), BlocProvider<MessagingCubit>(create: (context) => chatCubit), ], child: MultiProvider( providers: [ ChangeNotifierProvider(create: (_) => BottomSheetProvider()), ], child: const Application(), ), ), ); }, blocObserver: blocLogger, ); }