Skip to content

Commit b41b786

Browse files
committed
refactor(bloc_observer): simplify state change logging
- Reduce state information string length limit from 250 to 150 characters - Remove status property check and related logic - Simplify variable declarations using final instead of var
1 parent 0a827da commit b41b786

File tree

1 file changed

+5
-28
lines changed

1 file changed

+5
-28
lines changed

lib/bloc_observer.dart

Lines changed: 5 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -15,39 +15,16 @@ class AppBlocObserver extends BlocObserver {
1515

1616
// Initialize state information strings.
1717
// By default, truncate the full string representation of the state
18-
// to the first 250 characters to prevent excessively long logs.
19-
var oldStateInfo = oldState.toString().substring(
18+
// to the first 150 characters to prevent excessively long logs.
19+
final oldStateInfo = oldState.toString().substring(
2020
0,
21-
oldState.toString().length > 250 ? 250 : oldState.toString().length,
21+
oldState.toString().length > 150 ? 150 : oldState.toString().length,
2222
);
23-
var newStateInfo = newState.toString().substring(
23+
final newStateInfo = newState.toString().substring(
2424
0,
25-
newState.toString().length > 250 ? 250 : newState.toString().length,
25+
newState.toString().length > 150 ? 150 : newState.toString().length,
2626
);
2727

28-
try {
29-
// Attempt to access a 'status' property on the state objects.
30-
// Many BLoC states use a 'status' property (e.g., Loading, Success, Failure)
31-
// to represent their current lifecycle phase. If this property exists
32-
// and is not null, prioritize logging its value for conciseness.
33-
if (oldState.status != null) {
34-
oldStateInfo = 'status: ${oldState.status}';
35-
}
36-
if (newState.status != null) {
37-
newStateInfo = 'status: ${newState.status}';
38-
}
39-
} catch (e) {
40-
// This catch block handles cases where:
41-
// 1. The 'status' property does not exist on the state object (NoSuchMethodError).
42-
// 2. Accessing 'status' throws any other runtime error.
43-
// In such scenarios, the `oldStateInfo` and `newStateInfo` variables
44-
// will retain their initially truncated string representations,
45-
// providing a fallback for states without a 'status' property.
46-
// Log the error for debugging purposes, but do not rethrow to avoid
47-
// crashing the observer.
48-
log('Error accessing status property for ${bloc.runtimeType}: $e');
49-
}
50-
5128
// Log the state change, including the BLoC type and the old and new state information.
5229
log('onChange(${bloc.runtimeType}, $oldStateInfo -> $newStateInfo)');
5330
}

0 commit comments

Comments
 (0)