Home > Software engineering >  Flutter file_picker is returning null bytes
Flutter file_picker is returning null bytes

Time:09-10

I have the following code for file_picker. When I run it in a linux client, I get the following results. Is this a bug?

flutter: fatsquid.jpg
flutter: null
flutter: 497741
flutter: jpg
flutter: /home/mark/Pictures/fatsquid.jpg

I can't map form data from file.path as I am using flutter web.

FilePickerResult? result;
PlatformFile? file;

  selectFile() async {
    result = await FilePicker.platform.pickFiles(type: FileType.any);

    if (result != null) {
      file = result?.files.first;
    }

    print(file?.name);
    print(file?.bytes);
    print(file?.size);
    print(file?.extension);
    print(file?.path);

    setState(() {});
  }

file_picker: ^5.0.1

edit:

Applying the answer below, I get the following result.

flutter: fatsquid.jpg
flutter: 497741
flutter: 497741
flutter: jpg
flutter: /home/mark/Pictures/fatsquid.jpg

updated code:

result =
    await FilePicker.platform.pickFiles(type: FileType.any, withData: true);

CodePudding user response:

you need to set

withData: true

if you want to have the file loaded into memory in advance

  • Related