Flutter:未处理的异常:无法加载资产

Posted

技术标签:

【中文标题】Flutter:未处理的异常:无法加载资产【英文标题】:Flutter: Unhandled Exception: Unable to load asset 【发布时间】:2020-05-04 21:48:03 【问题描述】:

我正在尝试构建一个应用程序,其中一个按钮应该打开一个 pdf。我在此链接中使用了指南:https://pspdfkit.com/blog/2019/opening-a-pdf-in-flutter/ 但是它在应用程序第一次运行时不起作用,它仅在热重载/重启后才起作用。我试过颤振干净。 pubspec.yaml 的输出也是退出代码 0

flutter:
  assets:
    - PDFs/MyDoc.pdf
    - assets/

这是一个sn-p的代码:

import 'dart:io';
import 'dart:typed_data';

import 'package:flutter/material.dart';
import 'package:flutter_full_pdf_viewer/full_pdf_viewer_scaffold.dart';
import 'package:path_provider/path_provider.dart';

const String _documentPath = 'PDFs/MyDoc.pdf';

void main() => runApp(MaterialApp(
      home: FadyCard(),
    ));

class FadyCard extends StatefulWidget 
  @override
  _FadyCardState createState() => _FadyCardState();


class _FadyCardState extends State<FadyCard> 

  Future<String> prepareTestPdf() async 
    final ByteData bytes =
        await DefaultAssetBundle.of(context).load(_documentPath);
    final Uint8List list = bytes.buffer.asUint8List();

    final tempDir = await getTemporaryDirectory();
    final tempDocumentPath = '$tempDir.path/$_documentPath';

    final file = await File(tempDocumentPath).create(recursive: true);
    file.writeAsBytesSync(list);
    return tempDocumentPath;
  
  @override
  Widget build(BuildContext context) 
    return Scaffold(
      backgroundColor: Colors.grey[900],
      appBar: AppBar(
        title: Text('Document'),
        centerTitle: true,
        backgroundColor: Colors.grey[850],
        elevation: 0,
      ),
      bottomNavigationBar: BottomAppBar(
        shape: const CircularNotchedRectangle(),
        child: Container(
          height: 50.0,
        ),
        color: Colors.grey[850],
      ),
      floatingActionButton: FloatingActionButton(
         onPressed: () => 
        prepareTestPdf().then((path) 
          Navigator.push(
            context,
            MaterialPageRoute(
                builder: (context) => FullPdfViewerScreen(path)),
          );
        )
      ,
        child: Icon(Icons.folder, color: Colors.amber,),
        backgroundColor: Colors.grey[700],
      ),
      floatingActionButtonLocation: FloatingActionButtonLocation.centerDocked,
    );
  



class FullPdfViewerScreen extends StatelessWidget 
  final String pdfPath;

  FullPdfViewerScreen(this.pdfPath);

  @override
  Widget build(BuildContext context) 
    return PDFViewerScaffold(
        appBar: AppBar(
          title: Text("My Document"),
          backgroundColor: Colors.grey[800],
        ),
        path: pdfPath);
  

提前致谢。

【问题讨论】:

【参考方案1】:

FadyCard 是您需要在 setState 中更新 PDF 的 StateFull 小部件。 像这样试试(未测试)

onPressed: () => 
              setState(() 
                prepareTestPdf().then((path) 
                  Navigator.push(
                    context,
                    MaterialPageRoute(
                        builder: (context) => FullPdfViewerScreen(path)),
                  );
                )
              )
            

【讨论】:

以上是关于Flutter:未处理的异常:无法加载资产的主要内容,如果未能解决你的问题,请参考以下文章

Flutter:无法加载资产文件“car.png”

Flutter 无法在物理设备上加载图像资产(但在模拟器上加载它就好了)

Flutter:无法加载资产图像提供者:AssetImage(bundle:null,名称:“assets/images/rose.jpg”)

Flutter web 无法从包中加载资产

无法加载资产:

Flutter web:一些png资产未加载(404)状态