Flutter pageview url routing reorder loop

From divinevideo/divine-mobile · View interactive version

Flutter pageview url routing reorder loop is an AI agent skill from the GitHub repository divinevideo/divine-mobile. Fix infinite rebuild loop in Flutter when using PageView with URL-based routing (GoRouter) and reactive state management (Riverpod/BLoC). Use when: (1) RAPID REBUILD warnings appear in console (#20+), (2) "setState() or markNeedsBuild() called during build" errors from ValueListenableBuilder, (3) Vi

How to use this skill

Skill file

Flutter PageView + URL Routing Reorder Detection Loop

Problem

When a PageView is synced bidirectionally with URL-based routing (e.g., GoRouter /home/:index)

and the data source is a reactive provider (Riverpod, BLoC stream), attempting to track the

currently-viewed item by its ID and update the URL when the item moves to a different index

in the list creates an infinite feedback loop.

Context / Trigger Conditions

Symptoms:

Architecture that triggers this:

Example of the anti-pattern:

// IN BUILD METHOD - creates feedback loop!
if (_currentVideoStableId != null && videos.isNotEmpty) {
  final currentVideoIndex = videos.indexWhere(
    (v) => v.stableId == _currentVideoStableId,
  );
  if (currentVideoIndex != -1 && currentVideoIndex != urlIndex) {
    // This triggers URL update -> rebuild -> PageController sync ->
    // onPageChanged -> URL update -> INFINITE LOOP
    context.go('/home/$currentVideoIndex');
  }
}

Solution

Remove item-tracking reorder detection entirely. The PageController should be the sole

source of truth for which page the user is viewing.

Step 1: Remove tracking state

// REMOVE these fields:
// String? _currentVideoStableId;
// bool _urlUpdateScheduled = false;

Step 2: Remove reorder detection block

Remove any code in build() that:

Step 3: Keep only one-way sync patterns

User swipe -> URL (one-way):

onPageChanged: (newIndex) {
  if (newIndex != urlIndex) {
    context.go('/home/$newIndex');
  }
}

External navigation -> PageController (one-way):

if (urlIndex != _lastUrlIndex) {
  _lastUrlIndex = urlIndex;
  controller.jumpToPage(urlIndex);
}

These two paths don't create loops because:

The Feedback Loop Explained

┌─ Reactive provider re-emits (new data from server) ─┐
│                                                       │
▼                                                       │
Build runs with new video list                          │
│                                                       │
▼                                                       │
Reorder detection: "Video X moved from idx 4 to 3"     │
│                                                       │
▼                                                       │
Schedule URL update: context.go('/home/3')              │
│                                                       │
▼                                                       │
Build runs: urlIndex=3, PageController at page 4        │
│                                                       │
▼                                                       │
Sync: jumpToPage(3)                                     │
│                                                       │
▼                                                       │
onPageChanged(3) fires → context.go('/home/3')          │
│                                                       │
▼                                                       │
But PageController was at 4, which showed different     │
video → _currentVideoStableId mismatches → detects      │
"move" again → URL update to /home/4                    │
│                                                       │
└───────── INFINITE LOOP ──────────────────────────────┘

Verification

After removing reorder detection:

  1. No more RAPID REBUILD warnings in console
  2. No setState() called during build errors
  3. Swiping between pages works smoothly
  4. UI overlays stay visible on the active page
  5. No "moved from index X to Y" log spam

Notes

prepended), the user's current page index points to a different item. However, trying to

"follow" the item creates worse UX (infinite loop) than staying on the same page index.

stabilize the list order. Either:

PageView issue (snap-back on state rebuild) but can co-occur with this loop.

pattern applies to any PageView + URL routing + reactive state combination (Navigator 2.0,

auto_route, BLoC streams, etc.)

Use this skill with your AI agent: Open on Skill Issue to copy, download, or add to your library. Works with Claude, ChatGPT, Gemini, Cursor, Windsurf and more.