Shared Preferences Flutter - 根据书名保存

Shared Preferences Flutter - Save depending on Book Title

我正在开发一个 PDF 查看器,我试图将用户阅读的最后一页存储到共享首选项中作为 Book_Title id (tid)。当用户继续阅读时,他会根据每本书被重定向到他之前离开的地方。

我面临的问题是共享首选项没有保存到每个 Book_title id。它正在将最后一页保存到所有书籍中

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);
  }

完整代码

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),
    );
  }
}

好吧,在您 getPrefs 阅读当前书页之前,您 设置了 它。您不想在名为 getPrefs.

的方法中 set 首选项

只需从您的 getPrefs 方法中删除行 prefs.setInt('${book}', setPage);。那里还有很多其他错误,但这应该能让你继续。

我建议打开并收听您的linter。它会发现很多错误,并为您提供有关如何改进代码的良好提示。

您可以通过使用前缀键添加任何动态值来尝试此操作。

例如,

prefs.setInt("pageNo${bookId}", value);