title: How to save Epub reader progress in Flutter?
body: <p>In my Flutter project, I was implementing epub reader using following library -</p>
  <p><a href="https://pub.dev/packages/epub_viewer" rel="nofollow noreferrer">https://pub.dev/packages/epub_viewer</a></p>
  <p>I have done everything according to their library documentation. Downloading epub and opening the file works perfectly. But the problem is : I want to save the last read progress using cfi mentioned in the documentation but it's not working.</p>
  <p>Here's the entire code:</p>
  <pre><code>import 'dart:convert';
  import 'dart:io';
  
  import 'package:dio/dio.dart';
  import 'package:epub_viewer/epub_viewer.dart';
  import 'package:flutter/material.dart';
  import 'package:path_provider/path_provider.dart';
  import 'package:permission_handler/permission_handler.dart';
  
  void main() async {
    runApp(MyApp());
  }
  
  class MyApp extends StatefulWidget {
    @override
    _MyAppState createState() =&gt; _MyAppState();
  }
  
  class _MyAppState extends State&lt;MyApp&gt; {
    bool loading = false;
    Dio dio = new Dio();
  
    final imgUrl = &quot;http://bbebooksthailand.com/phpscripts/bbdownload.php?ebookdownload=CrimePunishment-EPUB2&quot;;
    bool downloading = false;
    var progressString = &quot;&quot;;
  
  
    @override
    void initState() {
      super.initState();
      download();
    }
  
    download() async {
      if (Platform.isAndroid) {
        print('download');
        await downloadFile();
      } else {
        loading = false;
      }
    }
  
    @override
    Widget build(BuildContext context) {
      return MaterialApp(
        home: Scaffold(
          appBar: AppBar(
            title: const Text('Plugin example app'),
          ),
          body: Center(
            child: downloading
                ? Container(
              height: 120.0,
              width: 200.0,
              child: Card(
                color: Colors.black,
                child: Column(
                  mainAxisAlignment: MainAxisAlignment.center,
                  children: &lt;Widget&gt;[
                    CircularProgressIndicator(),
                    SizedBox(
                      height: 20.0,
                    ),
                    Text(
                      &quot;Downloading File: $progressString&quot;,
                      style: TextStyle(
                        color: Colors.white,
                      ),
                    )
                  ],
                ),
              ),
            )
                : FlatButton(
              onPressed: () async {
                Directory appDocDir =
                await getApplicationDocumentsDirectory();
                print('$appDocDir');
  
                EpubViewer.setConfig(
                    themeColor: Theme.of(context).primaryColor,
                    identifier: &quot;androidBook&quot;,
                    scrollDirection: EpubScrollDirection.VERTICAL,
                    allowSharing: true,
                    enableTts: true,
                    nightMode: true);
  
                String androidBookPath = 'data/user/0/com.example.epub_flutter/app_flutter/book.epub';
  
                     EpubViewer.open(
  
                       Platform.isAndroid ? androidBookPath : androidBookPath,
                       lastLocation: EpubLocator.fromJson({
                         &quot;bookId&quot;: &quot;2239&quot;,
                         &quot;href&quot;: &quot;/OEBPS/ch06.xhtml&quot;,
                         &quot;created&quot;: 1539934158390,
                         &quot;locations&quot;: {
                           &quot;cfi&quot;: &quot;epubcfi(/0!/4/4[simple_book]/2/2/6)&quot;
                         }
                       }),
                     );
  
                EpubViewer.locatorStream.listen((locator) {
                  print(
                      'LOCATOR: ${locator.toString()}');
                });
  
              },
              child: Container(
                child: Text('open epub'),
              ),
            ),
          ),
        ),
      );
    }
  
    Future&lt;void&gt; downloadFile() async {
      Dio dio = Dio();
  
      try {
        var dir = await getApplicationDocumentsDirectory();
        print(&quot;path ${dir.path}&quot;);
        await dio.download(imgUrl, &quot;${dir.path}/book.epub&quot;,
            onReceiveProgress: (rec, total) {
              print(&quot;Rec: $rec , Total: $total&quot;);
  
              setState(() {
                downloading = true;
                progressString = ((rec / total) * 100).toStringAsFixed(0) + &quot;%&quot;;
              });
            });
      } catch (e) {
        print(e);
      }
  
      setState(() {
        downloading = false;
        progressString = &quot;Completed&quot;;
      });
      print(&quot;Download completed&quot;);
    }
  
    startDownload() async {
      Directory appDocDir = Platform.isAndroid
          ? await getExternalStorageDirectory()
          : await getApplicationDocumentsDirectory();
  
      String path = appDocDir.path + '/chair.epub';
      File file = File(path);
  
      if (!File(path).existsSync()) {
        await file.create();
        await dio.download(
          'https://github.com/FolioReader/FolioReaderKit/raw/master/Example/'
              'Shared/Sample%20eBooks/The%20Silver%20Chair.epub',
          path,
          deleteOnError: true,
          onReceiveProgress: (receivedBytes, totalBytes) {
            print((receivedBytes / totalBytes * 100).toStringAsFixed(0));
            //Check if download is complete and close the alert dialog
            if (receivedBytes == totalBytes) {
              loading = false;
              setState(() {});
            }
          },
        );
      } else {
        loading = false;
        setState(() {});
      }
    }
  }
  </code></pre>
  <p>The code snippet for locator is also not printing:</p>
  <pre><code> EpubViewer.locatorStream.listen((locator) {
                  print(
                      'LOCATOR: ${locator.toString()}');
                });
  </code></pre>
  <p>So, I need a solution to save my last read progress and open in that particular page where I left reading that epub.</p>