Skip to content

Commit 1a8386c

Browse files
committed
refactor(bloc_observer): improve state change logging
- Add detailed status information when available - Enhance log output with more specific state information - Maintain fallback to runtimeType when status is unavailable
1 parent 19d5e88 commit 1a8386c

File tree

2 files changed

+26
-6
lines changed

2 files changed

+26
-6
lines changed

lib/bloc_observer.dart

Lines changed: 23 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
// ignore_for_file: avoid_dynamic_calls
2+
13
import 'dart:developer';
24

35
import 'package:bloc/bloc.dart';
@@ -8,9 +10,27 @@ class AppBlocObserver extends BlocObserver {
810
@override
911
void onChange(BlocBase<dynamic> bloc, Change<dynamic> change) {
1012
super.onChange(bloc, change);
11-
log(
12-
'onChange(${bloc.runtimeType}, $change.currentState -> $change.nextState)',
13-
);
13+
final dynamic oldState = change.currentState;
14+
final dynamic newState = change.nextState;
15+
16+
var oldStateInfo = oldState.runtimeType.toString();
17+
var newStateInfo = newState.runtimeType.toString();
18+
19+
try {
20+
// Attempt to access a 'status' property if it exists
21+
if (oldState.status != null) {
22+
oldStateInfo = 'status: ${oldState.status}';
23+
}
24+
if (newState.status != null) {
25+
newStateInfo = 'status: ${newState.status}';
26+
}
27+
} catch (_) {
28+
// If 'status' property does not exist, or is null,
29+
// or if there's any other error accessing it,
30+
// fall back to runtimeType (which is already set).
31+
}
32+
33+
log('onChange(${bloc.runtimeType}, $oldStateInfo -> $newStateInfo)');
1434
}
1535

1636
@override

lib/bootstrap.dart

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -447,9 +447,9 @@ Future<Widget> bootstrap(
447447
inlineAdCacheService: inlineAdCacheService,
448448
initialUser: initialUser,
449449
localAdRepository: localAdRepository,
450-
navigatorKey: navigatorKey, // Pass the navigatorKey to App
451-
initialRemoteConfig: initialRemoteConfig, // Pass the initialRemoteConfig
450+
navigatorKey: navigatorKey,
451+
initialRemoteConfig: initialRemoteConfig,
452452
initialRemoteConfigError:
453-
initialRemoteConfigError, // Pass the initialRemoteConfigError
453+
initialRemoteConfigError,
454454
);
455455
}

0 commit comments

Comments
 (0)