packages.flutter icon indicating copy to clipboard operation
packages.flutter copied to clipboard

pdfx: fix error if rawDocumentProgress is NaN

Open akiller opened this issue 2 months ago • 0 comments

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

akiller avatar Nov 27 '25 12:11 akiller