gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-03 03:04:44 25 4
gpt4 key购买 nike

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

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

下面是一段代码:

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

提前致谢。

最佳答案

FadyCard 是 StateFul Widget,您需要在 setState 中更新 PDF。像这样尝试(未测试)

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

关于Flutter:未处理的异常:无法加载 Assets ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59796956/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com