Skip to content

RouteObserver example throws an error #141078

@heath3n

Description

@heath3n

Steps to reproduce

Paste the sample code found on the RouteObserver documentation page into your IDE and attempt to run it (tested on Windows, web, and Android).

Expected results

The code runs normally.

Actual results

The application loads a red error screen with the message:

======== Exception caught by widgets library =======================================================
The following assertion was thrown building _FocusInheritedScope:
A GlobalKey was used multiple times inside one widget's child list.

The offending GlobalKey was: [GlobalObjectKey<NavigatorState> _WidgetsAppState#f9d32]
The parent of the widgets with that key was: _FocusInheritedScope
The first child to get instantiated with that key became: Navigator-[GlobalObjectKey<NavigatorState> _WidgetsAppState#f9d32]
  dirty
  state: NavigatorState#d69af(lifecycle state: created)
The second child that was to be instantiated with that key was: _FocusInheritedScope
A GlobalKey can only be specified on one widget at a time in the widget tree.

Code sample

Code sample
// Register the RouteObserver as a navigation observer.
import 'package:flutter/material.dart';

final RouteObserver<ModalRoute<void>> routeObserver = RouteObserver<ModalRoute<void>>();

void main() {
  runApp(MaterialApp(
    home: Container(),
    navigatorObservers: <RouteObserver<ModalRoute<void>>>[ routeObserver ],
  ));
}

class RouteAwareWidget extends StatefulWidget {
  const RouteAwareWidget({super.key});

  @override
  State<RouteAwareWidget> createState() => RouteAwareWidgetState();
}

// Implement RouteAware in a widget's state and subscribe it to the RouteObserver.
class RouteAwareWidgetState extends State<RouteAwareWidget> with RouteAware {

  @override
  void didChangeDependencies() {
    super.didChangeDependencies();
    routeObserver.subscribe(this, ModalRoute.of(context)!);
  }

  @override
  void dispose() {
    routeObserver.unsubscribe(this);
    super.dispose();
  }

  @override
  void didPush() {
    // Route was pushed onto navigator and is now topmost route.
  }

  @override
  void didPopNext() {
    // Covering route was popped off the navigator.
  }

  @override
  Widget build(BuildContext context) => Container();

}

Screenshots or Video

Screenshots / Video demonstration

[Upload media here]

Logs

Logs
[Paste your logs here]

Flutter Doctor output

Doctor output
[√] Flutter (Channel stable, 3.16.5, on Microsoft Windows [Version 10.0.19045.3803], locale en-US)
    • Flutter version 3.16.5 on channel stable at C:\Development\flutter                          
    • Upstream repository https://github.com/flutter/flutter.git                                  
    • Framework revision 78666c8dc5 (3 weeks ago), 2023-12-19 16:14:14 -0800                      
    • Engine revision 3f3e560236                                                                  
    • Dart version 3.2.3                                                  
    • DevTools version 2.28.4                                             
                                                                          
[√] Windows Version (Installed version of Windows is version 10 or higher)
                                                                          
[√] Android toolchain - develop for Android devices (Android SDK version 34.0.0)
    • Android SDK at C:\Users\Matt\AppData\Local\Android\sdk                    
    • Platform android-34, build-tools 34.0.0                                   
    • Java binary at: C:\Users\Matt\AppData\Local\Programs\Android Studio\jbr\bin\java
    • Java version OpenJDK Runtime Environment (build 17.0.7+0-b2043.56-10550314)
    • All Android licenses accepted.

[√] Chrome - develop for the web
    • Chrome at C:\Program Files\Google\Chrome\Application\chrome.exe

[√] Visual Studio - develop Windows apps (Visual Studio Community 2022 17.6.5)
    • Visual Studio at C:\Program Files\Microsoft Visual Studio\2022\Community
    • Visual Studio Community 2022 version 17.6.33829.357
    • Windows 10 SDK version 10.0.22000.0

[√] Android Studio (version 2023.1)
    • Android Studio at C:\Users\Matt\AppData\Local\Programs\Android Studio
    • Flutter plugin can be installed from:
       https://plugins.jetbrains.com/plugin/9212-flutter
    • Dart plugin can be installed from:
       https://plugins.jetbrains.com/plugin/6351-dart
    • Java version OpenJDK Runtime Environment (build 17.0.7+0-b2043.56-10550314)

[√] IntelliJ IDEA Ultimate Edition (version 2023.1)
    • IntelliJ at C:\Users\Matt\AppData\Local\Programs\IntelliJ IDEA Ultimate
    • Flutter plugin can be installed from:
       https://plugins.jetbrains.com/plugin/9212-flutter
    • Dart plugin can be installed from:
       https://plugins.jetbrains.com/plugin/6351-dart

[√] VS Code (version 1.79.2)
    • VS Code at C:\Users\Matt\AppData\Local\Programs\Microsoft VS Code
    • Flutter extension can be installed from:
       https://marketplace.visualstudio.com/items?itemName=Dart-Code.flutter

[√] Connected device (4 available)
    • Android SDK built for x86 (mobile) • emulator-5554 • android-x86    • Android 8.1.0 (API 27) (emulator)
    • Windows (desktop)                  • windows       • windows-x64    • Microsoft Windows [Version 10.0.19045.3803]
    • Chrome (web)                       • chrome        • web-javascript • Google Chrome 120.0.6099.130
    • Edge (web)                         • edge          • web-javascript • Microsoft Edge 120.0.2210.121

[√] Network resources
    • All expected network resources are available.

• No issues found!

Metadata

Metadata

Assignees

Labels

c: crashStack traces logged to the consoled: examplesSample code and demosf: routesNavigator, Router, and related APIs.found in release: 3.16Found to occur in 3.16found in release: 3.18Found to occur in 3.18frameworkflutter/packages/flutter repository. See also f: labels.has reproducible stepsThe issue has been confirmed reproducible and is ready to work onr: fixedIssue is closed as already fixed in a newer versionteam-frameworkOwned by Framework team

Type

No type

Projects

Status

Done (PR merged)

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions