I'm working on a PDF viewer, where I'm trying to store the last page a user read to Shared Preferences as the Book_Title id (tid). When the user resumes reading he's redirected to where he previously left off depending on each book.
The problem I'm facing is the shared Preferences is not being saved to each Book_title id. It's saving the last page to all the books
setInt(Book_title ID, setPage)
var book;
int bookPage = 1;
int setPage = 1;
@override
void initState() {
book = '${widget.tid}';
_pdfViewerController = PdfViewerController();
getPref();
super.initState();
}
getPref() async {
SharedPreferences prefs = await SharedPreferences.getInstance();
prefs.setInt('${book}', setPage);
bookPage = prefs.getInt('${book}')!;
print(book);
}
Full code
class libraryPDF extends StatefulWidget {
final String? sid;
final String? tid;
final String? page;
const libraryPDF({Key? key, this.sid, this.page, this.tid}) : super(key: key);
@override
State<libraryPDF> createState() => _libraryPDFState();
}
var book;
int bookPage = 1;
int setPage = 1;
class _libraryPDFState extends State<libraryPDF> {
late PdfViewerController _pdfViewerController;
final GlobalKey<SfPdfViewerState> _pdfViewerStateKey = GlobalKey();
@override
void initState() {
book = '${widget.tid}';
_pdfViewerController = PdfViewerController();
getPref();
super.initState();
}
getPref() async {
SharedPreferences prefs = await SharedPreferences.getInstance();
await prefs.setInt('${book}', setPage);
bookPage = await prefs.getInt('${book}')!;
print(book);
}
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
centerTitle: true,
elevation: 0,
title: const Text("CanadianHS"),
backgroundColor: Colors.blue[900],
actions: <Widget>[
IconButton(
onPressed: () {
_pdfViewerStateKey.currentState!.openBookmarkView();
},
icon: Icon(
Icons.bookmark,
color: Colors.white,
)),
IconButton(
icon: Icon(
Icons.keyboard_arrow_left,
color: Colors.white,
),
onPressed: () {
_pdfViewerController.previousPage();
},
),
IconButton(
icon: Icon(
Icons.keyboard_arrow_right,
color: Colors.white,
),
onPressed: () {
_pdfViewerController.nextPage();
},
)
],
),
body: SfPdfViewer.network(
'https://url/${widget.sid}',
controller: _pdfViewerController,
onDocumentLoaded: (PdfDocumentLoadedDetails details) {
_pdfViewerController.jumpToPage(bookPage);
print(details.document.pages.count);
},
key: _pdfViewerStateKey,
onPageChanged: (PdfPageChangedDetails details) {
setPage = details.newPageNumber;
},
pageLayoutMode: PdfPageLayoutMode.single),
);
}
}
CodePudding user response:
Well, in your getPrefs
before you read the page of the current book, you set it. You don't want to set the preferences in a method called getPrefs.
Just remove the line prefs.setInt('${book}', setPage);
from your getPrefs
method. There are a lot of other bugs in there, but that should get you going.
I suggest turning on and listening to your linter. It will catch a lot of what is wrong and give you good hints on how to improve your code.
CodePudding user response:
You can try this by adding any dynamic values with the prefixed keys.
Such as,
prefs.setInt("pageNo${bookId}", value);