@@ -8,6 +8,11 @@ import 'package:instabug_flutter/src/utils/screen_loading/screen_loading_manager
88import 'package:instabug_flutter/src/utils/screen_name_masker.dart' ;
99
1010class InstabugNavigatorObserver extends NavigatorObserver {
11+ InstabugNavigatorObserver ({Duration ? screenReportDelay})
12+ : _screenReportDelay =
13+ screenReportDelay ?? const Duration (milliseconds: 100 );
14+
15+ final Duration _screenReportDelay;
1116 final List <InstabugRoute > _steps = [];
1217
1318 void screenChanged (Route newRoute) {
@@ -24,22 +29,29 @@ class InstabugNavigatorObserver extends NavigatorObserver {
2429 );
2530 //ignore: invalid_null_aware_operator
2631 WidgetsBinding .instance? .addPostFrameCallback ((_) async {
27- // Starts a the new UI trace which is exclusive to screen loading
28- ScreenLoadingManager .I .startUiTrace (maskedScreenName, screenName);
29- // If there is a step that hasn't been pushed yet
30- if (_steps.isNotEmpty) {
31- await reportScreenChange (_steps.last.name);
32- // Report the last step and remove it from the list
33- _steps.removeLast ();
34- }
32+ try {
33+ // Starts a the new UI trace which is exclusive to screen loading
34+ ScreenLoadingManager .I .startUiTrace (maskedScreenName, screenName);
35+ // If there is a step that hasn't been pushed yet
36+ final pendingStep = _steps.isNotEmpty ? _steps.last : null ;
37+ if (pendingStep != null ) {
38+ await reportScreenChange (pendingStep.name);
39+ // Remove the specific pending step regardless of current ordering
40+ _steps.remove (pendingStep);
41+ }
3542
36- // Add the new step to the list
37- _steps.add (route);
43+ // Add the new step to the list
44+ _steps.add (route);
3845
39- // If this route is in the array, report it and remove it from the list
40- if (_steps.contains (route)) {
41- await reportScreenChange (route.name);
42- _steps.remove (route);
46+ // If this route is in the array, report it and remove it from the list
47+ if (_steps.contains (route)) {
48+ await reportScreenChange (route.name);
49+ _steps.remove (route);
50+ }
51+ } catch (e) {
52+ InstabugLogger .I
53+ .e ('Reporting screen change failed:' , tag: Instabug .tag);
54+ InstabugLogger .I .e (e.toString (), tag: Instabug .tag);
4355 }
4456 });
4557 } catch (e) {
@@ -50,7 +62,7 @@ class InstabugNavigatorObserver extends NavigatorObserver {
5062
5163 Future <void > reportScreenChange (String name) async {
5264 // Wait for the animation to complete
53- await Future .delayed (const Duration (milliseconds : 100 ) );
65+ await Future .delayed (_screenReportDelay );
5466
5567 Instabug .reportScreenChange (name);
5668 }
0 commit comments