customblocobserver 1.0.1
customblocobserver: ^1.0.1 copied to clipboard
A custom Bloc Observer to keep track of your Bloc
CustomizableBlocObserver is a Dart package that provides a customizable observer for Flutter BLoC ( Business Logic Component) architecture. It allows developers to easily debug and monitor events, state changes, transitions, and errors within their BLoC implementations.
Features #
- Customizable Debugging: Enable or disable debugging for events, states, transitions, and errors.
- Detailed Logging: Logs detailed information about events, state changes, and transitions, including line numbers and stack traces.
- Error Handling: Captures and logs errors with detailed stack traces for easier debugging.
- Easy Integration: Seamlessly integrates with existing Flutter BLoC setups.
Getting started #
To start using CustomizableBlocObserver, follow these steps:
-
Add Dependency: Add the package to your
pubspec.yamlfile.dependencies: flutter_bloc: ^8.0.0 customizable_bloc_observer: ^1.0.0
Usage #
Just add the dependency and write the code below to start using the
import 'package:flutter/material.dart';
import 'package:flutter_bloc/flutter_bloc.dart';
import 'package:customizable_bloc_observer/customizable_bloc_observer.dart';
void main() {
Bloc.observer = CustomizableBlocObserver(
isDebugEnabled: true,
debugShowEvent: true,
debugShowState: true,
debugShowTransition: true,
);
runApp(MyApp());
}
Available Methods #
OnEvent #
@override
void onEvent(Bloc bloc, Object? event) {
super.onEvent(bloc, event);
if (isDebugEnabled && debugShowEvent) {
final stackTrace = StackTrace.current;
final frame = stackTrace.toString().split('\n')[3]; // Adjust index to capture developer's code
final lineNumber = frame
.split(' ')
.last
.replaceAll(')', '');
log('📢 Event Triggered: $event at line $lineNumber');
}
}
onError #
Here are some methods available
@override
void onError(BlocBase bloc, Object error, StackTrace stackTrace) {
super.onError(bloc, error, stackTrace);
if (isDebugEnabled) {
log('💥 Error in ${bloc.runtimeType}: $error', error: error, stackTrace: stackTrace);
}
}
onTransition #
Here are some methods available
@override
void onTransition(Bloc bloc, Transition transition) {
super.onTransition(bloc, transition);
if (isDebugEnabled && debugShowTransition) {
log(' ➡️ Transition: ${transition.currentState} → ${transition.nextState}');
}
}
onChange #
Here are some methods available
@override
void onChange(BlocBase bloc, Change change) {
super.onChange(bloc, change);
if (isDebugEnabled && debugShowState) {
log(' 🔄 State Changed: ${change.nextState}');
}
}
noSuchMethod #
Here are some methods available
@override
dynamic noSuchMethod(Invocation invocation) {
if (isDebugEnabled) {
log('⚠️ No such method: ${invocation.memberName}');
}
return super.noSuchMethod(invocation);
}
Additional information #
This README provides a clear and concise overview of your package, its features, and how to get started