如何在 flutter 中从应用程序目录加载 pdf 文件?

How to load a pdf file from Application Directory in flutter?

我正在尝试从应用程序目录加载 PDF 文件,我尝试从 url 加载它并且它有效,我不需要从应用程序目录加载已经下载的 PDF。

这是我用来查看 PDF flutter_pdfview 的包,在尝试从 url 加载它时效果很好,现在我需要的是从目录加载已下载的 pdf .

为了从目录加载文件,我试过这个:


var dir = await getApplicationDocumentsDirectory();
File file = File('${dir.path}/$pName.pdf');

bool  fileExists = File(await '${dir.path}/$pName.pdf')
                  .existsSync();

             if(fileExists)
                {
                  urlPdfPath = file.toString();
                  print('url pdf path $urlPdfPath');
                  Navigator.push(context, MaterialPageRoute(builder: (context) {
                    return PdfViewer(
                      path: urlPdfPath,
                      product: pName,
                    );
                  }));

                }


当我 运行 我遇到了异常

D/AndroidRuntime( 4565): Shutting down VM
E/AndroidRuntime( 4565): FATAL EXCEPTION: main
E/AndroidRuntime( 4565): java.lang.IllegalArgumentException: Unsupported value: java.io.FileNotFoundException: No such file or directory

在您的代码中:

将 - urlPdfPath = file.toString(); 更改为 urlPdfPath = file.path;