簡體   English   中英

Flutter 應用程序在使用 flutter_pdfview package 時崩潰

[英]Flutter app crashing while using flutter_pdfview package

Package: flutter_pdfview

當 pdf 下載在我使用flutter apk build 構建的 apk 中完成時,我的應用程序崩潰。

在下載完成后調試的同一操作中,控制台會出錯但不會崩潰,並且 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(),
    ));
  }
}

我在 StackOverflow 鏈接上發現了一個類似的問題。

當我們從代碼構建 apk 時,Flutter 會阻止插件代碼,從而導致應用程序崩潰。 解決方案是使用 progaurd 規則來啟用 pdf 插件工作。 鏈接中提到了 ProGaurd 規則。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM