Skip to content

pasteboard.image: when system clipboard content changed, then it will crash #414

@function2000

Description

@function2000

It is OK to get the image when there is image content in the system clipboard, but when I change the content, like I make another screenshot, then I cannot get the updated image, and it will crash my app.

Please help to check and solve it.

Thanks a lot!

Below is my code which is used together with Flutter_quill

Future _handlePasteImage() async {
try {
Uint8List? imageBytes = await Pasteboard.image;

  if (imageBytes == null || imageBytes.isEmpty) {
    debugPrint("no valid pic");
    return;
  }

  
  final decodedImage = await decodeImageFromList(imageBytes);
  if (decodedImage.width <= 0 || decodedImage.height <= 0) {
    debugPrint("no valid pic");
    return;
  }


  final dir = await getApplicationDocumentsDirectory();
  final fileName = 'pasted_${DateTime.now().millisecondsSinceEpoch}.png';
  final file = File('${dir.path}/$fileName');

  await file.writeAsBytes(imageBytes); 
  debugPrint("saved: ${file.path}");


  final imageUrl = file.path;
  final selection = Get.find<NoteViewController>().quill_controller.selection;
  final index = selection.baseOffset < 0 ? 0 : selection.baseOffset;

  Get.find<NoteViewController>().quill_controller.document.insert(index, BlockEmbed.image(imageUrl));
  Get.find<NoteViewController>().quill_controller.updateSelection(
    TextSelection.collapsed(offset: index + 1),
    ChangeSource.local,
  );
} catch (e, stack) {
  debugPrint("error: $e");
  debugPrint("$stack");
  if (mounted) {
    ScaffoldMessenger.of(context).showSnackBar(
      const SnackBar(content: Text("error")),
    );
  }
}

}

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions