简体   繁体   中英

Flutter app crash on release mode reading PDF

I have a pdf reader in my app, with this function I get de pdf from URL and save the file in local path

  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");
    }
  }

After this process, I call a class to show this PDF in a new route

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()
        ],
      ),
    );
  }
}

All works fine in debug mode, but when I run my app on release, the app crashes in CustomPdfView.

I don't know what is the error, I already added, STORAGE permissions in my /app/src/main/AndroidManifest.xml

<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />

And I don't know how can I get error line in the console, because the app is running on release.

same issue..... i fixed that one

  1. clean your project after build your apk

if still problem persist than try these commands

flutter build apk --no-shrink

flutter build apk --release --split-per-abi --no-shrink

your way of reading pdf is very nice, here you try to launch pdf in your app,

i almost did the same way.

where i was getting Uint8List from url of pdf, and reading it in my app using printing 5.9.3

but when size of pdf or number of pages more then my app keep getting crash, and also before launching pdf in my app i need to save it locally as you did.

so, to solve this i just need to use flutter_cached_pdfview: ^0.4.1

using this flutter_cached_pdfview i don't need to save pdf locally, just pass the url of pdf and you can launch it in your app without worrying about size of pdf or number of pages in pdf.

i just launch using a button and i pass url of pdf

  // navigate in another screen
   onPressed: () => nextScreen(
                          PDFViewerPage(
                            url: widget.magazineModel.fileUrl,
                            title: widget.magazineModel.name,
                          ),
                        ),

and MyPdfViewerPage is below

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",
          );
        },
      ),
    );
  }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM