在使用
下载完成后,在调试时的相同操作中,控制台会出现错误,但不会崩溃,并且pdf_viewer可以正常工作。
W/System (31159): A resource failed to call release.
请建议下载文件后,我该如何阻止我的应用崩溃。
代码是这样的
import 'dart:async';
import 'package:firebase_storage/firebase_storage.dart';
import 'package:flutter/material.dart';
import 'package:flutter_pdfview/flutter_pdfview.dart';
import 'package:fluttertoast/fluttertoast.dart';
import 'dart:io';
import 'package:path_provider/path_provider.dart';
class PdfViewerPage extends StatefulWidget {
final name;
final title;
PdfViewerPage({this.name, this.title});
@override
_PdfViewerPageState createState() => _PdfViewerPageState();
}
class _PdfViewerPageState extends State<PdfViewerPage> {
bool loading = true;
String localfile;
int current = 1;
int total;
Future<String> loaddata() async {
var dir = await getTemporaryDirectory();
File file = new File(dir.path + widget.name);
await FirebaseStorage()
.ref()
.child(widget.name)
.getData(50000000)
.then((value) async {
file.writeAsBytes(value);
}).catchError((onError) {
Fluttertoast.showToast(msg: onError);
});
return file.path;
}
@override
void initState() {
super.initState();
loaddata().then((value) {
setState(() {
localfile = value;
loading = false;
});
});
}
@override
Widget build(BuildContext context) {
return SafeArea(
child: Scaffold(
appBar: AppBar(
title: Text(widget.title, style: TextStyle(color: Colors.black)),
),
body: loading
? Center(
child: CircularProgressIndicator(),
)
: PDFView(
filePath: localfile,
fitEachPage: true,
onPageChanged: (t, c) {
setState(() {
current = t + 1;
total = c;
});
},
),
floatingActionButton: total != null
? FloatingActionButton.extended(
backgroundColor: Colors.white,
onPressed: () {},
label: Row(
mainAxisAlignment: MainAxisAlignment.spaceAround,
children: <Widget>[
Text(current.toString()),
SizedBox(
width: 20,
),
Text(total.toString())
],
))
: Container(),
));
}
}
答案 0 :(得分:0)
我在StackOverflow link上发现了类似的问题。
当我们从代码构建apk时,Flutter阻止了插件代码,这导致应用程序崩溃。 解决方案是使用progaurd规则来启用pdf插件。链接中提到了ProGaurd规则。