Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Running page render on Isolate throw 'Null check operator used on a null value' #299

Open
ncvescera opened this issue May 17, 2022 · 2 comments
Assignees
Labels
bug Something isn't working

Comments

@ncvescera
Copy link

ncvescera commented May 17, 2022

Describe the bug
I am trying to render all the pdf's pages in the background via Dart Isolate but when I call the render function (page.render()) I have this error: Null check operator used on a null value. Is that a bug or am I doing something wrong ??

Platform: Android
Flutter Version: 2.10.5

Full error message:

E/flutter ( 9510): [ERROR:flutter/runtime/dart_isolate.cc(1111)] Unhandled exception:
E/flutter ( 9510): Null check operator used on a null value
E/flutter ( 9510): #0      BasicMessageChannel.binaryMessenger                   package:flutter/…/services/platform_channel.dart:52
E/flutter ( 9510): #1      BasicMessageChannel.send                              package:flutter/…/services/platform_channel.dart:60
E/flutter ( 9510): #2      PdfxApi.renderPage                                    package:pdfx/…/io/pigeon.dart:634
E/flutter ( 9510): #3      PdfPageImagePigeon.render                             package:pdfx/…/io/platform_pigeon.dart:253
E/flutter ( 9510): #4      PdfPagePigeon.render.<anonymous closure>              package:pdfx/…/io/platform_pigeon.dart:158
E/flutter ( 9510): #5      PdfPagePigeon.render.<anonymous closure>              package:pdfx/…/io/platform_pigeon.dart:151
E/flutter ( 9510): #6      BasicLock.synchronized                                package:synchronized/src/basic_lock.dart:31
E/flutter ( 9510): #7      PdfPagePigeon.render                                  package:pdfx/…/io/platform_pigeon.dart:151
E/flutter ( 9510): #8      renderer                                              package:keep_reading/API/pdf_manager.dart:116
E/flutter ( 9510): <asynchronous suspension>

My code:

void preloadPdfPages(String filePath, Function callback) async {
  final ReceivePort receivePort = ReceivePort();
  await Isolate.spawn(renderer, receivePort.sendPort);

  var sendPort = await receivePort.first;

  final document = await PdfDocument.openFile(filePath); // Pages start at 1
  final totalPages = document.pagesCount;

  List<Image> pages = <Image>[];

  for (int i = 1; i <= totalPages; i++) {
    PdfPage page = await document.getPage(i);

    PdfPageImage? pageImage =
        await sendReceive(sendPort, page) as PdfPageImage?;

    if (pageImage != null) {
      pages.add(
        Image(
          image: MemoryImage(pageImage.bytes),
        ),
      );
    }

    await page.close();
  }

  await sendReceive(sendPort, null); // Close the isolate
  await document.close();

  callback(pages);
}

// the entry point for the isolate
void renderer(SendPort sendPort) async {
  // Open the ReceivePort for incoming messages.
  ReceivePort port = ReceivePort();

  // Notify any other isolates what port this isolate listens to.
  sendPort.send(port.sendPort);

  await for (var msg in port) {
    var data = msg[0];
    SendPort replyTo = msg[1];

    if (data == null) {
      port.close();
    }

    PdfPage page = data as PdfPage;
    debugPrint('w: ${page.width} h: ${page.height}');

    final double width = page.width * 2;
    final double height = page.height * 2;

    PdfPageImage? pageImage = await page.render(   // error here
      width: width,
      height: height,
      format: PdfPageImageFormat.png,
      backgroundColor: '#ffffff',
    );

    replyTo.send(pageImage);
  }
}

/// sends a message on a port, receives the response,
/// and returns the message
Future sendReceive(SendPort port, msg) {
  ReceivePort response = ReceivePort();
  port.send([msg, response.sendPort]);
  return response.first;
}
@ncvescera ncvescera added the bug Something isn't working label May 17, 2022
@SergeShkurko
Copy link
Member

SergeShkurko commented May 21, 2022

Hi
Platform Android / iOS?
Flutter version?

@ncvescera
Copy link
Author

Sorry, I forgot to add this on the issue !

I am on Android with Flutter 2.10.5

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

2 participants