Using PdfViewer.data the pdf doesn't update if I change data and sourceName
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?
Any news about this bug? Any plan for the fix?
Have you tried specifying the key: property for the widget?
Yes, I tried with a UniqueKeybut with no luck:
child: pdfrx.PdfViewer.data(
_pdfData!,
key: UniqueKey(),
@cyberneid try using ValueKey and pass sourceName as a value
key: ValueKey<String?>(widget.fileName)
@espresso3389 pdfrx: ^1.2.7 has the same bug too. Any plan for the fix? thank you
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',
),
);
}
}
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(() {});