首页 > 解决方案 > Flutter:如何防止在调试控制台中打印异常?

问题描述

我正在使用 try catch 进行错误处理并在出现错误时显示小部件。这有效,但调试控制台中仍会打印异常,这种情况下的异常是IOException,如何防止此异常在控制台中打印?

try {
    PDFDocument pdfDocument = await PDFDocument.fromURL(pdfUrl);
    Navigator.of(context).push(
      MaterialPageRoute(
        builder: (ctx) =>
            DisplayPdf(pdfDocument),
      ),
    );
} catch (error) {
    showDialog(...);
}

fromURL我正在使用的pdf包中的函数代码:

static Future<PDFDocument> fromURL(String url) async {
    // Download into cache
    File f = await DefaultCacheManager().getSingleFile(url);
    PDFDocument document = PDFDocument();
    document._filePath = f.path;
    try {
      var pageCount =
          await _channel.invokeMethod('getNumberOfPages', {'filePath': f.path});
      document.count = document.count = int.parse(pageCount);
    } catch (e) {
      throw Exception('Error reading PDF!');
    }
    return document;
  }

标签: flutterdart

解决方案


推荐阅读