Unhandled Exception: Rangeerror (index): Index out of range: index should be Less than

Asked

Viewed 322 times

0

I’m having a problem trying to save a pdf using the "pdf_dart" library. I assume the problem is linked to the image I’m downloading from the internet to try to attach to the pdf but I’m not sure what it is.

CODE

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

The error happens right at the time when I will save the pdf already with the image IE, apparently the image is downloaded normally, but when inserting it in the pdf and trying to save is caused the error.

I don’t know if the way I am saving is the right one, maybe it is this, but unfortunately I can’t access the file folder without being going all the way where it will be saved (if you give me a solution to this I would also appreciate but it is not the biggest problem in question).

THE STACKTRACE OF THE ERROR

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):

I did a test recently and realized that the problem is why I try to save pdf within "then" of image return.

The error is not necessarily linked to the image (I don’t know if this information is useful).

If someone can show me another alternative to recover an image, either from the internet or from "Assets" and be able to put it in the pdf I would be grateful.

  • @Drinjer is yes :/

  • Tried with any other image? With smaller size(bytes)

  • I tried Drinjer. I haven’t figured out the problem yet, but it’s not totally related to the image (as I commented below). When I try to create the PDF "INSIDE" of http "Then", it gives this problem. It doesn’t matter if there is an image or not.

  • 1

    Try to display the image on screen normally, in a Container, Image or something like, to see if she’s really being returned correctly.

No answers

Browser other questions tagged

You are not signed in. Login or sign up in order to post.