gpt4 book ai didi

flutter - 未处理的异常 : RangeError (index): Index out of range: index should be less than

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

尝试使用“pdf_dart”库保存 pdf 时遇到问题。
我认为问题与我从互联网下载以尝试附加到 pdf 的图像有关,但我不确定它是什么。

代码

import 'dart:io';
import 'package:app_formulario/models/Customer.dart';
import 'package:path_provider/path_provider.dart';
import 'package:pdf/pdf.dart' as pdfDart;
import 'package:pdf/widgets.dart';
import 'package:image/image.dart' as imageDart;
import 'package:flutter/material.dart' as materialDart;
import 'package:http/http.dart' as http;

class PDFBloc {
PDFBloc() {
final Document pdf = Document();

http
.get(
"https://clsp.com.br/wp-content/uploads/2018/05/logo-clsp-500px.png")
.then((onValue) {
final image = pdfDart.PdfImage(
pdf.document,
image: onValue.bodyBytes,
width: 300,
height: 150,
);

pdf.addPage(Page(build: (Context context) {
return Center(
child: Image(image),
); // Center
}));

criarPDF(pdf);
});
}

void criarPDF(Document pdf) async {
Directory appDocDir = await getApplicationDocumentsDirectory();
String appDocPath = appDocDir.path;
appDocPath = "/storage/emulated/0/Android/data/com.rwrd.app_formulario";

final File file = File('$appDocPath/example.pdf');
file.writeAsBytesSync(pdf.save());
}

错误发生在我要保存带有图像的pdf时,也就是说,显然图像已正常下载,但是当将其插入pdf并尝试保存时,会导致错误。

我不知道我保存的方式是否正确,也许就是这样,但不幸的是,我无法在不完全浏览文件夹的情况下访问该文件夹。

错误堆栈
E/flutter (18670): [ERROR:flutter/lib/ui/ui_dart_state.cc(157)] Unhandled Exception: RangeError (index): Index out of range: index should be less than 6307: 6308
E/flutter (18670): #0 Uint8List.[] (dart:typed_data-patch/typed_data_patch.dart:2209:7)
E/flutter (18670): #1 PdfImage._prepare
package:pdf/src/image.dart:183
E/flutter (18670): #2 PdfObject._write
package:pdf/src/object.dart:53
E/flutter (18670): #3 PdfOutput.write
package:pdf/src/output.dart:69
E/flutter (18670): #4 SetMixin.forEach (dart:collection/set.dart:148:30)
E/flutter (18670): #5 PdfDocument._write
package:pdf/src/document.dart:168
E/flutter (18670): #6 PdfDocument.save
package:pdf/src/document.dart:176
E/flutter (18670): #7 Document.save
package:pdf/widgets/document.dart:71
E/flutter (18670): #8 PDFBloc.criarPDF
package:app_formulario/blocs/pdfBloc.dart:53
E/flutter (18670): <asynchronous suspension>
E/flutter (18670): #9 new PDFBloc.<anonymous closure>
package:app_formulario/blocs/pdfBloc.dart:35
E/flutter (18670): #10 _rootRunUnary (dart:async/zone.dart:1134:38)
E/flutter (18670): #11 _CustomZone.runUnary (dart:async/zone.dart:1031:19)
E/flutter (18670): #12 _FutureListener.handleValue (dart:async/future_impl.dart:139:18)
E/flutter (18670): #13 Future._propagateToListeners.handleValueCallback (dart:async/future_impl.dart:680:45)
E/flutter (18670): #14 Future._propagateToListeners (dart:async/future_impl.dart:709:32)
E/flutter (18670): #15 Future._completeWithValue (dart:async/future_impl.dart:524:5)
E/flutter (18670): #16 _AsyncAwaitCompleter.complete (dart:async-patch/async_patch.dart:32:15)
E/flutter (18670): #17 _completeOnAsyncReturn (dart:async-patch/async_patch.dart:290:13)
E/flutter (18670): #18 _withClient (package:http/http.dart)
E/flutter (18670): <asynchronous suspension>
E/flutter (18670): #19 get
package:http/http.dart:46
E/flutter (18670): #20 new PDFBloc
package:app_formulario/blocs/pdfBloc.dart:18
E/flutter (18670): #21 FormBloc._recoverCustomerInformations
package:app_formulario/blocs/formBloc.dart:37
E/flutter (18670): #22 _rootRunUnary (dart:async/zone.dart:1134:38)
E/flutter (18670): #23 _CustomZone.runUnary (dart:async/zone.dart:1031:19)
E/flutter (18670): #24 _CustomZone.runUnaryGuarded (dart:async/zone.dart:933:7)
E/flutter (18670): #25 _BufferingStreamSubscription._sendData (dart:async/stream_impl.dart:338:11)
E/flutter (18670): #26 _DelayedData.perform (dart:async/stream_impl.dart:593:14)
E/flutter (18670): #27 _StreamImplEvents.handleNext (dart:async/stream_impl.dart:709:11)
E/flutter (18670): #28 _PendingEvents.schedule.<anonymous closure> (dart:async/stream_impl.dart:669:7)
E/flutter (18670): #29 _rootRun (dart:async/zone.dart:1122:38)
E/flutter (18670): #30 _CustomZone.run (dart:async/zone.dart:1023:19)
E/flutter (18670): #31 _CustomZone.runGuarded (dart:async/zone.dart:925:7)
E/flutter (18670): #32 _CustomZone.bindCallbackGuarded.<anonymous closure> (dart:async/zone.dart:965:23)
E/flutter (18670): #33 _rootRun (dart:async/zone.dart:1126:13)
E/flutter (18670): #34 _CustomZone.run (dart:async/zone.dart:1023:19)
E/flutter (18670): #35 _CustomZone.runGuarded (dart:async/zone.dart:925:7)
E/flutter (18670): #36 _CustomZone.bindCallbackGuarded.<anonymous closure> (dart:async/zone.dart:965:23)
E/flutter (18670): #37 _microtaskLoop (dart:async/schedule_microtask.dart:43:21)
E/flutter (18670): #38 _startMicrotaskLoop (dart:async/schedule_microtask.dart:52:5)
E/flutter (18670):

最佳答案

将pdf图像小部件更改为此,

final image = pdfDart.PdfImage.jpeg(
pdf.document,
image: onValue.bodyBytes,
);

现在,您的图像应该出现在 pdf 中,但是如果您想固定高度和宽度,则必须调整图像大小。

关于flutter - 未处理的异常 : RangeError (index): Index out of range: index should be less than,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60126504/

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