DART: PlatformFile Imagefile is not null but Imagefile.bytes is null. Unable to load image into pdf

23 Views Asked by At

I'm trying to load an image which was selected using the filepicker from a previous function (_selectImage()) call into a pdf in the generatepdf function. Even though the variable imageFile is not null , imageFile.bytes is null which is causing an error and not allowing me to load the image into the pdf.(this is for a windows desktop app)

FilePickerResult? result;
  Future<void> _selectImage() async {
    result = await FilePicker.platform.pickFiles(
      type: FileType.image,
    );

    if (result != null) {
      setState(() {
        _selectedImage = File(result!.files.single.path!);
        _prediction = '';
        _confidence; // Clear any previous prediction.
      });
    }
  }

 Future<void> generatePDFWithContent(
      FilePickerResult? result1, String prediction, double confidence) async {
    final pdf = pw.Document();

   //pdf generation...
          // Insert the selected image into the PDF
          if (result1 != null && result1.files.isNotEmpty) {
            final imageFile = result1.files.first;

            final Uint8List imageData = imageFile.bytes!;  // this part returns the error
            content.add(pw.Image(pw.MemoryImage(imageData)));
          
          }

          // Create a column containing all the content.
          return pw.Column(
  // rest of code

Any help would be greatly appreciated.

0

There are 0 best solutions below