Heikkisorsa
Heikkisorsa

Reputation: 905

Deep link to push screen if stack is available

I'm using the go_router package for reacting to deep links to my app. During a user process I'm calling an external website and by the end this website calls a link that is associated with my app using deep links. It works, but GoRouter just can "go" to the new screen I defined instead of pushing it like the rest of the user process does.

I use this definition for the deep link capture:

      GoRoute(
        path: 'callback',
        builder: (context, state) {
          // Optional status parameter in case of error
          final String? status = state.uri.queryParameters['status'];

          return CallbackView(status: status);
        },
      ),

How can I tell GoRouter that it should push the CallbackView if a stack is available and only use go if otherwise?

I tried this already and it seems to work, except when I want to direct back I get an Issue about offending calls and MediaQuery:

  builder: (context, state) {
          // Optional status parameter in case of error
          final String? status = state.uri.queryParameters['status'];

          // Check if Navigator has any routes
          if (Navigator.of(context).canPop()) {
            // If yes, push the new route
            Navigator.of(context).push(
              MaterialPageRoute(
                builder: (BuildContext context) =>
                    CallbackView(status: status),
              ),
            );
            // Return a minimal widget
            return const SizedBox.shrink();
          } else {
            // If no, return the new route
            return CallbackView(status: status);
          }
        }

Issue:

════════ Exception caught by widgets library ═══════════════════════════════════
The following assertion was thrown building Builder(dirty):
setState() or markNeedsBuild() called during build.

This Overlay widget cannot be marked as needing to build because the framework is already in the process of building widgets. A widget can be marked as needing to be built during the build phase only if one of its ancestors is currently building. This exception is allowed because the framework builds parent widgets before children, which means a dirty descendant will always be built. Otherwise, the framework might not visit this widget during this build phase.
The widget on which setState() or markNeedsBuild() was called was: Overlay-[LabeledGlobalKey<OverlayState>#6d0b1]
    state: OverlayState#b0bf0(entries: [OverlayEntry#bbff6(opaque: true; maintainState: false), OverlayEntry#39ec4(opaque: false; maintainState: true), OverlayEntry#9bc80(opaque: true; maintainState: false), OverlayEntry#8ebed(opaque: false; maintainState: true), OverlayEntry#a8c5e(opaque: true; maintainState: false), OverlayEntry#0bc15(opaque: false; maintainState: true), OverlayEntry#cd201(opaque: true; maintainState: false), OverlayEntry#192bd(opaque: false; maintainState: true), OverlayEntry#82c05(opaque: true; maintainState: false), OverlayEntry#b21bd(opaque: false; maintainState: true), OverlayEntry#16cb2(opaque: true; maintainState: false), OverlayEntry#28aea(opaque: false; maintainState: true)])
The widget which was currently being built when the offending call was made was: Builder
The relevant error-causing widget was
MediaQuery

Upvotes: 1

Views: 459

Answers (1)

Cyrille Dakhlia
Cyrille Dakhlia

Reputation: 407

I've seen in this good article that push is not recommended with deep linking, whereas go is the option to use. Maybe that's why it seems to be used, but it is just a supposition.

Otherwise, you have to keep in mind that using "go will modify the underlying navigation stack if the new route is not a sub-route of the old one." , as said in the article. You can find an example of this behaviour on this post.

Upvotes: 0

Related Questions