【问题标题】:Flutter: Unhandled Exception: Unable to load assetFlutter:未处理的异常:无法加载资产
【发布时间】: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);
  }
}

提前致谢。

【问题讨论】:

    标签: flutter dart load assets


    【解决方案1】:

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

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2021-07-14
      • 1970-01-01
      • 2020-04-28
      • 2021-05-10
      • 2020-04-11
      • 2020-10-12
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多