A user taps a universal link in a push notification, the app launches from a killed state, and lands on the home screen. Or worse, on the login screen, and after signing in, on a detail page for a different item than the one they tapped. We have seen this exact bug in codebases we've audited, and it's almost never a go_router bug. It's a race, and Flutter deep links on cold start lose it in three or four distinct ways.

This post dissects that race and walks through the pending-intent pattern we use to make it deterministic.

Why Flutter deep links on cold start are a race

On a cold start, three independent things resolve on their own timelines:

  1. The platform delivers the link. On iOS, the URL arrives via the scene or app delegate (continueUserActivity); on Android, via the launch intent. Flutter surfaces it to the framework as the initial route, through PlatformDispatcher.defaultRouteName or the route information channel.
  2. The router builds. go_router parses the incoming location and runs its redirect against it, usually during the first frame.
  3. Auth state resolves. Reading a token from secure storage, validating or refreshing it, loading the user profile. This is async, and it's often the slowest of the three.

The link is available at step 1. The auth answer isn't available until step 3. Most apps wire the router so that step 2 runs before step 3 finishes, and the redirect has to decide something with an unknown auth state.

The three failure modes

1. The redirect treats "unknown" as "logged out"

The common shape looks like this:

redirect: (context, state) {
  final loggedIn = ref.read(authProvider).valueOrNull != null;
  if (!loggedIn) return '/login';
  return null;
}

While the auth provider is still loading, valueOrNull is null, so every cold-start link is redirected to /login. The original location is discarded unless you carried it in a query parameter, and most apps don't. The link is dropped, silently.

2. The redirect is re-run, but the target is gone

Fix the first problem naively by adding refreshListenable on the auth state and the router re-evaluates when auth resolves. But by then the current location is /login, not the deep link. Auth succeeds, the redirect sends the user to /home, and the link is still lost. The router has no memory of what the user originally asked for.

3. The link is replayed on the wrong screen

The subtler variant: an app stashes the link in a global or a singleton, then replays it from a listener when auth completes. If that listener fires on a token refresh, on a second login, or after the user has already navigated elsewhere, you get a stale replay. We've seen a link consumed twice, and we've seen a link from a previous session pushed on top of a freshly logged-in different account. That last one is a data-exposure bug, not just a UX one.

The pending-intent pattern

The fix is to treat a deep link as an intent with a lifecycle, not as a route that must be resolved immediately. The rules:

  • Auth state is a sealed type with an explicit unknown member, never a nullable user.
  • While auth is unknown, the router shows a neutral splash and makes no destination decision.
  • If the target requires auth and the user is signed out, the intended location is saved as a single pending intent, then the user is sent to login.
  • The intent is consumed exactly once, after auth is authenticated, and cleared.

In go_router terms, the redirect becomes a pure function of three inputs: auth state, the requested location, and the pending intent.

redirect: (context, state) {
  final auth = authNotifier.value;       // Unknown | SignedOut | SignedIn
  final loc = state.matchedLocation;

  if (auth is Unknown) {
    return loc == '/splash' ? null : '/splash';  // hold, don't decide
  }
  if (auth is SignedOut) {
    if (isPublic(loc)) return null;
    pending.set(state.uri.toString());           // remember, once
    return '/login';
  }
  // SignedIn
  if (loc == '/splash' || loc == '/login') {
    return pending.take() ?? '/home';            // consume exactly once
  }
  return null;
}

One catch: when auth is unknown and you redirect to /splash, the original location is lost the same way as in failure mode 1. So the unknown branch should also record the incoming URI if it isn't /splash. Keep that write idempotent: only set the pending intent if none exists, so a later internal redirect can't overwrite the real link with /login.

Details that make it hold up

  • Validate on take, not on set. The intent may be attacker-influenced (it came from a URL). Check the path against your route allow-list and the user's permissions when you consume it, because the account may differ from when it was saved.
  • Clear on logout and account switch. A pending intent belongs to the session that created it. Wiping it in the sign-out path closes the cross-account replay.
  • Don't persist it across process death unless you mean to. In-memory is enough for the cold-start case. Persisting it turns a one-time handoff into something that can resurface days later.
  • Give auth a timeout. If token validation hangs on a bad network, unknown must resolve to something, usually SignedOut with the intent preserved, rather than leaving a permanent splash.

How to test it deterministically

The reason this bug survives code review is that it depends on timing, so the test has to control timing. Make the auth resolver injectable and drive it with a Completer. Then write three cases: link arrives before auth resolves to signed-in; link arrives before auth resolves to signed-out, then login; and link arrives, user logs out, a different user logs in. Assert the final location and that the intent store is empty. These run as plain widget tests, no emulator needed.

It's also worth testing on real devices with the app force-quit, since warm-start and cold-start take different paths through the platform layer. On the native side, make sure the Swift and Kotlin entry points aren't consuming or rewriting the link before Flutter sees it. That's a common culprit when a plugin and your own handler both claim the same URL.

Closing thought

Routing and auth are each simple on their own; the bugs live in the ordering between them. If you're staring at a deep-link bug that only reproduces on a killed app, or you want a second pair of eyes on your router and auth architecture, orithLabs does this kind of code audit and troubleshooting, and you'd be talking directly to the engineers who write the fix.