Skip to content

GoRouter throws Location cannot be empty error when using context.replace or context.pushReplacement, the router ends up in a broken state and the refresh listenable no longer works. #149951

@iSaqibShafique

Description

@iSaqibShafique

Steps to reproduce

If Firebase user is null or not verified, then the navigation works perfectly, however when the user try to login, this error occurs.
// ! Error : Location cannot be empty.

Expected results

Navigate to dashboard when user logged in.

Actual results

Giving "Location cannot be empty" error.

Code sample

Code sample

Code :

class AppRoutes {
  static String splash = "/";
  static String login = "/login";
  static String register = "/register";
  static String maindashboardview = "/maindashboardview";
  static String forgotpassword = "/forgotpassword";
  static String emailnotverified = "/emailnotverified";
  static GlobalKey<NavigatorState> key = GlobalKey<NavigatorState>();
  static GoRouter router = GoRouter(
    routes: [
      GoRoute(
        path: splash,
        name: splash,
        builder: (context, state) => const LoadingScreenView(),
        redirect: (context, state) {
          User? user =
              FirebaseAuthService().currentuser.toValueNotifier().value;
          if (user != null && user.emailVerified) {
            return maindashboardview;
          } else if (user != null && user.emailVerified == false) {
            return emailnotverified;
          } else if (user == null) {
            return login;
          } else {
            return null;
          }
        },
      ),
      GoRoute(
        path: login,
        name: login,
        builder: (context, state) => const LoginView(),
      ),
      GoRoute(
        path: register,
        name: register,
        builder: (context, state) => const RegisterView(),
      ),
      GoRoute(
        path: maindashboardview,
        name: maindashboardview,
        builder: (context, state) => const DashboardView(),
      ),
      GoRoute(
        path: forgotpassword,
        name: forgotpassword,
        builder: (context, state) => const ForgotPasswordView(),
      ),
      GoRoute(
        path: emailnotverified,
        name: emailnotverified,
        builder: (context, state) => const EmailNotVerifiedScreen(),
      ),
    ],
    refreshListenable: FirebaseAuthService().currentUser.toValueListenable(),
    debugLogDiagnostics: true,
    initialLocation: "/",
    navigatorKey: key,
  );
}


//! Refresh Listener.

StreamSignal<User?> currentUser = StreamSignal(
    () {
      final FirebaseAuth auth = FirebaseAuth.instance;
      return auth.authStateChanges().map((event) {
        return event;
      });
    },
  );

Screenshots or Video

Screenshots / Video demonstration

[Upload media here]

Logs

Logs
════════ Exception caught by foundation library ════════════════════════════════
The following GoException was thrown while dispatching notifications for GoRouteInformationProvider:
Location cannot be empty.

When the exception was thrown, this was the stack:
#0      canonicalUri (package:go_router/src/path_utils.dart:125:5)
path_utils.dart:125
#1      RouteConfiguration.findMatch (package:go_router/src/configuration.dart:296:31)
configuration.dart:296
#2      _RouteMatchListDecoder.convert (package:go_router/src/match.dart:947:23)
match.dart:947
#3      Codec.decode (dart:convert/codec.dart:30:34)
codec.dart:30
#4      GoRouteInformationParser.parseRouteInformationWithDependencies (package:go_router/src/parser.dart:71:32)
parser.dart:71
#5      _RouterState._processRouteInformation (package:flutter/src/widgets/router.dart:749:8)
router.dart:749
#6      _RouterState._handleRouteInformationProviderNotification (package:flutter/src/widgets/router.dart:767:5)
router.dart:767
#7      ChangeNotifier.notifyListeners (package:flutter/src/foundation/change_notifier.dart:433:24)
change_notifier.dart:433
#8      GoRouteInformationProvider.notifyListeners (package:go_router/src/information_provider.dart:134:11)
information_provider.dart:134
#9      ChangeNotifier.notifyListeners (package:flutter/src/foundation/change_notifier.dart:433:24)
change_notifier.dart:433
#10     ValueNotifier.value= (package:flutter/src/foundation/change_notifier.dart:555:5)
change_notifier.dart:555
#11     new SignalValueListenable.merge.<anonymous closure> (package:signals_flutter/src/core/signal_value_listenable.dart:22:40)
signal_value_listenable.dart:22
#12     ReadonlySignal.subscribe.<anonymous closure> (package:signals_core/src/core/readonly.dart:94:11)
readonly.dart:94
#13     Effect._callback (package:signals_core/src/core/effect.dart:350:32)
effect.dart:350
#14     _endBatch (package:signals_core/src/core/batch.dart:29:18)
batch.dart:29
#15     batch (package:signals_core/src/core/batch.dart:121:5)
batch.dart:121
#16     AsyncSignal.setValue (package:signals_core/src/async/signal.dart:201:5)
signal.dart:201
(elided 36 frames from dart:async)

</details>

Flutter Doctor output

Doctor output

[✓] Flutter (Channel stable, 3.19.6, on macOS 14.5 23F79 darwin-x64, locale
en-AE)
• Flutter version 3.19.6 on channel stable at /Users/mac/development/flutter
• Upstream repository https://github.com/flutter/flutter.git
• Framework revision 54e6646 (7 weeks ago), 2024-04-17 13:08:03 -0700
• Engine revision c4cd48e186
• Dart version 3.3.4
• DevTools version 2.31.1

[✓] Android toolchain - develop for Android devices (Android SDK version 34.0.0)
• Android SDK at /Users/mac/Library/Android/sdk
• Platform android-34, build-tools 34.0.0
• Java binary at: /Applications/Android
Studio.app/Contents/jbr/Contents/Home/bin/java
• Java version OpenJDK Runtime Environment (build
17.0.6+0-17.0.6b829.9-10027231)
• All Android licenses accepted.

[✓] Xcode - develop for iOS and macOS (Xcode 15.3)
• Xcode at /Applications/Xcode.app/Contents/Developer
• Build 15E204a
• CocoaPods version 1.13.0

[✓] Chrome - develop for the web
• Chrome at /Applications/Google Chrome.app/Contents/MacOS/Google Chrome

[✓] Android Studio (version 2022.3)
• Android Studio at /Applications/Android Studio.app/Contents
• 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.6+0-17.0.6b829.9-10027231)

[✓] VS Code (version 1.90.0)
• VS Code at /Applications/Visual Studio Code.app/Contents
• Flutter extension version 3.90.0

[✓] Connected device (3 available)
• Small Device (mobile) • C4716D87-4028-4E82-9B49-7A74C91003C1 • ios
• com.apple.CoreSimulator.SimRuntime.iOS-17-4 (simulator)
• macOS (desktop) • macos • darwin-x64
• macOS 14.5 23F79 darwin-x64
• Chrome (web) • chrome •
web-javascript • Google Chrome 125.0.6422.142
! Error: Browsing on the local area network for iPhone 13 pro max. Ensure
the device is unlocked and attached with a cable or associated with the
same local area network as this Mac.
The device must be opted into Developer Mode to connect wirelessly. (code
-27)
! Error: Browsing on the local area network for iPhone. Ensure the device is
unlocked and attached with a cable or associated with the same local area
network as this Mac.
The device must be opted into Developer Mode to connect wirelessly. (code
-27)

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

• No issues found!

Metadata

Metadata

Assignees

Labels

P1High-priority issues at the top of the work listfound in release: 3.22Found to occur in 3.22found in release: 3.23Found to occur in 3.23has reproducible stepsThe issue has been confirmed reproducible and is ready to work onp: go_routerThe go_router packagepackageflutter/packages repository. See also p: labels.r: fixedIssue is closed as already fixed in a newer version

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions