fix: skip full viewer reset on progress-only notifications#617
Open
i4innovationnet wants to merge 1 commit intoespresso3389:masterfrom
Open
fix: skip full viewer reset on progress-only notifications#617i4innovationnet wants to merge 1 commit intoespresso3389:masterfrom
i4innovationnet wants to merge 1 commit intoespresso3389:masterfrom
Conversation
PdfDocumentListenable._progress() calls notifyListeners() on every downloaded HTTP chunk during range-access loading. The viewer's _onDocumentChanged listener unconditionally releases all cached page images (releaseAllImages), clears layout, and sets _initialized=false on EVERY notification — even when the document reference hasn't changed. With preferRangeAccess enabled on a large PDF, hundreds of chunks arrive during and after loading. Each triggers a full nuclear reset, causing rendered pages to flash white repeatedly and preventing stable rendering. Fix: add an early return at the top of _onDocumentChanged() that skips the reset when the document reference is the same object. This correctly distinguishes progress-only notifications from actual document changes (initial load, document swap, error/null).
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Problem
When using
preferRangeAccess: trueon large PDFs (≥10 MB), the viewer repeatedly flashes white and fails to render pages stably. Pages load, go white, re-load in an infinite loop until all byte ranges are cached.Root cause
PdfDocumentListenable._progress()(pdf_document_ref.dart:513-516) callsnotifyListeners()on every downloaded HTTP chunk:The viewer's
_onDocumentChanged()(pdf_viewer.dart:324) is registered as a listener and unconditionally releases all cached page images, clears layout, and sets_initialized = false— before checking if the document reference actually changed:After
loadDocument()returns, PDFium continues reading blocks on-demand for rendering visible pages (via the captured read callback inPdfFileCache). These reads trigger_progress()→notifyListeners()→ full reset, creating an infinite loop where rendering triggers downloads which destroy the renders.Reproduction
PdfViewer.uri(uri, preferRangeAccess: true)orPdfViewer(PdfDocumentRefUri(uri, preferRangeAccess: true))Test results (iPad Pro, 22.3 MB / 18-page PDF over Firebase Storage)
Without this fix (stock pdfrx 2.2.24, no workaround):
notifyListeners()calls, 2,858 post-loadreleaseAllImages,_initialized = false)onViewerReadyfired 7+ times — the viewer kept getting destroyed and re-initialized in a loopWith this fix (same PDF, same device, same network):
notifyListeners()calls, 2,858 post-load (same notification volume)onViewerReadyfired onceRelated issues
_onDocumentChanged()fired unnecessarily (due toPdfDocumentRefequality comparison changes). Fixed in v2.2.9 by comparing.keyinstead of the ref object. Same class of bug: unnecessary full resets in_onDocumentChanged().Fix
Add a 3-line early return at the top of
_onDocumentChanged()that skips the reset when the document reference is the same object:Edge cases (all verified safe)
currentDoc_documentdidUpdateWidgetsame docImpact
preferRangeAccess: trueon large PDFs