pdfrx icon indicating copy to clipboard operation
pdfrx copied to clipboard

Using PdfViewer.data the pdf doesn't update if I change data and sourceName

Open cyberneid opened this issue 11 months ago • 4 comments

I added the PdfViewer to my app using pdf as data (UInt8List), but if later I update the pdf data and the sourceName the pdf is not updated in the widget and I still see the old pdf. This is my code;:

child: pdfrx.PdfViewer.data(
    widget.pdfData!,
    sourceName: widget.fileName!,
    controller: _controller,
    ...
),

Later in my code, I do this:

widget.pdfData = Uint8List.fromList(await document.save());
widget.fileName = "signed_${widget.fileName!}";
setState(() {});

I expect to see the new updated pdf in the viewer, but I see the old one.

In one of the previous versions of pdfrx (1.0.67) it worked. now, with 1.0.91, and later, it doesn't (I just tried with 101).

Can you fix it?

cyberneid avatar Jan 27 '25 15:01 cyberneid

Any news about this bug? Any plan for the fix?

cyberneid avatar Jan 30 '25 11:01 cyberneid

Have you tried specifying the key: property for the widget?

vanyasem avatar Jan 31 '25 17:01 vanyasem

Yes, I tried with a UniqueKeybut with no luck:

child: pdfrx.PdfViewer.data(
     _pdfData!,
      key: UniqueKey(),

cyberneid avatar Jan 31 '25 17:01 cyberneid

@cyberneid try using ValueKey and pass sourceName as a value

key: ValueKey<String?>(widget.fileName)

vanyasem avatar Feb 01 '25 14:02 vanyasem

@espresso3389 pdfrx: ^1.2.7 has the same bug too. Any plan for the fix? thank you

paladinghub avatar Jul 20 '25 12:07 paladinghub

I've tested with such a simple program and it worked well.

class MainPage extends StatefulWidget {
  const MainPage({super.key});

  @override
  State<MainPage> createState() => _MainPageState();
}

class _MainPageState extends State<MainPage> {
  bool useA = true;
  final a = File(r'C:\Users\kawasaki\Downloads\a.pdf').readAsBytesSync();
  final b = File(r'C:\Users\kawasaki\Downloads\b.pdf').readAsBytesSync();

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: const Text('Pdfrx example'),
        actions: [
          IconButton(
            icon: Icon(useA ? Icons.toggle_on : Icons.toggle_off),
            onPressed: () {
              setState(() {
                useA = !useA;
              });
            },
          ),
        ],
      ),
      body: PdfViewer.data(
        useA ? a : b,
        sourceName: useA ? 'a' : 'b',
      ),
    );
  }
}

espresso3389 avatar Jul 20 '25 15:07 espresso3389

Ah, I see the point. sourceName is not a name of the file name. It is a key to uniquely identify the data source. pdfrx does not reload the data if sourceName is not changed.

So, if you want to reload the data, you should do like the following fragment:

int _count = 0; // something it make the sourceName unique.

widget.pdfData = Uint8List.fromList(await document.save());
widget.fileName = "${++_count}:signed_${widget.fileName!}"; // count make the sourceName unique
setState(() {});

espresso3389 avatar Jul 20 '25 15:07 espresso3389