The argument type 'Uint8List' can't be assigned to the parameter type 'File'

98 Views Asked by At

[The argument type 'Uint8List' can't be assigned to the parameter type 'File'.dartargument_type_not_assignable Uint8List? bytes Type: Uint8List?]

Reference image - (https://i.stack.imgur.com/iSbTP.png)

My Code:

IconButton(
    onPressed: () async {
        Uint8List? bytes = await screenshotController.capture();
        imageProvider.changeImage(bytes!);
        if (!mounted) return;
        Navigator.of(context).pop();

        },
        icon: const Icon(Icons.done),
)

Can someone suggest how to solve this error please? I am a beginner

I tried to pass bytes as parameter with null check, but this isn't working

https://www.youtube.com/watch?v=qXU5o8v4DX0&ab_channel=KODDev

I was coding after which this yt video. It seems to be working for him (Time stamp - 52:25), but not for me.

1

There are 1 best solutions below

2
user14193305 On

Hi you need to convert the Uint8List to a File before passing it to the IconButton.

Future<void> saveImage(Uint8List bytes) async {
  Directory appDocDir = await getApplicationDocumentsDirectory();
  String filePath = '${appDocDir.path}/temp_image.png';
  File tempFile = File(filePath);

  await tempFile.writeAsBytes(bytes);

  // Now, you can use tempFile as a File
  imageProvider.changeImage(tempFile);

  if (!mounted) return;
  Navigator.of(context).pop();
}

// Inside your IconButton's onPressed callback
Uint8List? bytes = await screenshotController.capture();
await saveImage(bytes!);

Kellil Amine