Skip to content

pdfx: fix error if rawDocumentProgress is NaN - #602

Closed
akiller wants to merge 1 commit into
ScerIO:mainfrom
akiller:main
Closed

pdfx: fix error if rawDocumentProgress is NaN#602
akiller wants to merge 1 commit into
ScerIO:mainfrom
akiller:main

Conversation

@akiller

@akiller akiller commented Nov 27, 2025

Copy link
Copy Markdown

Using pdfx 2.9.2, and also when using the latest changes on main.

I'm seeing the PDF widget render as a white block on iOS, or very occasionally a red Flutter error:

It seems to be fine if using Axis.vertical but not when using horizontal scrolling:

PdfViewPinch(
    controller: pdfControllerPinch,
    builders: PdfViewPinchBuilders<DefaultBuilderOptions>(
      options: const DefaultBuilderOptions(
        loaderSwitchDuration: Duration(seconds: 1),
      ),
      documentLoaderBuilder: (_) =>
          const Center(child: CircularProgressIndicator()),
      pageLoaderBuilder: (_) =>
          const Center(child: CircularProgressIndicator()),
      errorBuilder: (_, error) => Center(child: Text(error.toString())),
    ),
    scrollDirection: Axis.horizontal,
  ),
)
image image

The error in my logs is:

flutter: ├┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄
flutter: │ ⛔ [ERROR] [main] Flutter framework error
flutter: └───────────────────────────────────────────────────────────────────────────────────────────────────────────────────────
flutter: ┌───────────────────────────────────────────────────────────────────────────────────────────────────────────────────────
flutter: │ Unsupported operation: Infinity or NaN toInt
flutter: ├┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄
flutter: │ #3   _PdfViewPinchState._reLayout.<anonymous closure> (package:pdfx/src/viewer/pinch/pdf_view_pinch.dart:234:11)
pdf_view_pinch.dart:234
flutter: │ #4   new Future.delayed.<anonymous closure> (dart:async/future.dart:419:42)
future.dart:419
flutter: │ #5   _rootRun (dart:async/zone.dart:1517:47)
zone.dart:1517
flutter: │ #6   _CustomZone.run (dart:async/zone.dart:1422:19)
zone.dart:1422
flutter: │ #7   _CustomZone.runGuarded (dart:async/zone.dart:1321:7)

Which through debugging is caused by line 298 of pdf_view_pinch.dart inside _determinePagesToShow():

 _controller._documentProgress =
     ((rawDocumentProgress * precisionFactor).round() / precisionFactor)
         .clamp(0.0, 1.0);

In my case, the rawDocumentProgress calculation ended up being (503.15789473684214 - 503.15789473684214) / (503.15789473684214 - 503.15789473684214) = 0/0 which results in a NaN in Flutter.

image

This PR adds a check to set the progress to 0 if rawDocumentProgress is NaN.

It seems to work OK, but I have no idea if this will have any negative effects elsewhere

Thanks

@vicajilau

Copy link
Copy Markdown
Member

This looks like the same NaN/Infinity guard as #621 (opened as a fresh fork of this one) and a related but different approach in #604. To avoid merging the same fix twice, we'll pick one of #602/#621 to merge and close the others as duplicates. Will follow up here.

vicajilau added a commit that referenced this pull request Aug 20, 2026
…inch (#621)

* fix(pdfx): evita crash "Infinity or NaN toInt" en PdfViewPinch

Cuando el documento cabe exactamente en el viewport (o cuando ambos
miden 0 durante el primer frame en Android edge-to-edge / gesture nav),
el divisor (_docSize.height - _lastViewSize.height) es 0 y produce
NaN/Infinity. Al llegar a .round().toInt() lanza UnsupportedError y el
PDF se queda en blanco.

Se normaliza rawDocumentProgress a 0.0 cuando es NaN o Infinite antes
de aplicar .round(): no hay recorrido pendiente si el documento cabe
en pantalla.

Basado en el PR #602 de akiller (upstream, sin merge desde nov 2025).
Repro: Pixel 10a Android 15/16, pdfx 2.9.2.

* Translate guard comment to English

* Apply dart format

---------

Co-authored-by: Victor Carreras <34163765+vicajilau@users.noreply.github.com>
@vicajilau

Copy link
Copy Markdown
Member

Following up on the earlier comment: we merged #621, which has the same NaN/Infinity guard as this PR plus an updated branch and an explanatory comment. Closing this as a duplicate. Thanks for the fix, and sorry for the delay in following up.

@vicajilau vicajilau closed this Aug 20, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants