[英]Flutter app crash on release mode reading PDF
我的應用程序中有一個 pdf 閱讀器,有了這個 function,我從 URL 得到了 de pdf 並將文件保存在本地路徑中
Future<File> getFileFromUrl(String url) async {
try {
var data = await http.get(url);
var bytes = data.bodyBytes;
var dir = await getApplicationSupportDirectory();
File file = File("${dir.path}/some.pdf");
File urlFile = await file.writeAsBytes(bytes);
return urlFile;
} catch (e) {
throw Exception("Error opening url file");
}
}
在此過程之后,我調用 class 以在新路線中顯示此 PDF
import 'package:flutter/material.dart';
import 'package:flutter_pdfview/flutter_pdfview.dart';
class CustomPdfView extends StatefulWidget {
final String title;
final String urlPdf;
CustomPdfView(
this.title,
this.urlPdf
);
@override
_CustomPdfViewState createState() => _CustomPdfViewState();
}
class _CustomPdfViewState extends State<CustomPdfView> {
//int _totalPages = 0;
//int _currentPage = 0;
bool pdfReady = false;
//PDFViewController _pdfViewController;
@override
Widget build(BuildContext context) {
print('Aqui entra antes: ${widget.urlPdf}');
return Scaffold(
appBar: AppBar(
title: Text(widget.title),
),
body: Stack(
children: <Widget>[
PDFView(
filePath: widget.urlPdf,
autoSpacing: true,
enableSwipe: true,
pageSnap: true,
swipeHorizontal: true,
nightMode: false,
onError: (e) {
print("error $e");
},
onRender: (_pages) {
setState(() {
//_totalPages = _pages;
pdfReady = true;
});
},
onViewCreated: (PDFViewController vc) {
//_pdfViewController = vc;
},
onPageChanged: (int page, int total) {
setState(() {});
},
onPageError: (page, e) {},
),
!pdfReady
? Center(
child: CircularProgressIndicator(),
)
: Offstage()
],
),
);
}
}
在調試模式下一切正常,但當我在發布時運行我的應用程序時,應用程序在 CustomPdfView 中崩潰。
我不知道是什么錯誤,我已經在 /app/src/main/AndroidManifest.xml 中添加了 STORAGE 權限
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
而且我不知道如何在控制台中獲取錯誤行,因為該應用程序正在發布時運行。
同樣的問題......我修復了那個
如果問題仍然存在,請嘗試這些命令
flutter build apk --no-shrink
flutter build apk --release --split-per-abi --no-shrink
您閱讀 pdf 的方式非常好,在這里您嘗試在您的應用程序中啟動 pdf,
我幾乎也是這樣做的。
我從 pdf 的 url 獲取 Uint8List,並使用打印 5.9.3在我的應用程序中讀取它
但是當 pdf 的大小或頁數超過我的應用程序時,我的應用程序不斷崩潰,而且在我的應用程序中啟動 pdf 之前,我需要像你一樣將它保存在本地。
所以,為了解決這個問題,我只需要使用flutter_cached_pdfview: ^0.4.1
使用此 flutter_cached_pdfview 我不需要在本地保存 pdf,只需傳遞 pdf 的 url,您就可以在您的應用程序中啟動它,而不必擔心 pdf 的大小或 pdf 中的頁數。
我只是使用一個按鈕啟動,我通過了 url of pdf
// navigate in another screen
onPressed: () => nextScreen(
PDFViewerPage(
url: widget.magazineModel.fileUrl,
title: widget.magazineModel.name,
),
),
MyPdfViewerPage 在下面
class PDFViewerPage extends StatelessWidget {
final String url;
final String title;
const PDFViewerPage({
Key? key,
required this.title,
required this.url,
}) : super(key: key);
@override
Widget build(BuildContext context) {
debugPrint('url : $url');
return Scaffold(
appBar: AppBar(
leading: const LeadingIconComponent(), // for popScreen()
title: Text(title),
),
body: const PDF().cachedFromUrl(
Uri.parse(url).toString(),
placeholder: (progress) => const LoadingComponent(),
errorWidget: (error) {
debugPrint('errorInOprnPDF : $error');
return const ErrorPageComponent(
title: 'File Not Exist',
content: "This file is either removed or not valid",
image: "assets/images/empty_data_component/new data not found.svg",
);
},
),
);
}
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.