miguelpruivo/flutter_file_picker

Issue: Unable to Read File Content (CSV)

Closed this issue · 1 comments

Issue: Unable to Read File Content

Description:
I am encountering an issue where the readStream and bytes properties are both null after successfully picking a CSV file using the FilePicker plugin.

Steps to Reproduce:

  1. Select a CSV file using the FilePicker plugin.
  2. Attempt to read the file content.

Expected Behavior:
The readStream or bytes properties should contain the file content.

Actual Behavior:
Both readStream and bytes are null.

Additional Information:

  • Flutter version: Issue: Unable to Read File Content

Description:
I am encountering an issue where the readStream and bytes properties are both null after successfully picking a CSV file using the FilePicker plugin.

Steps to Reproduce:

  1. Select a CSV file using the FilePicker plugin.
  2. Attempt to read the file content.

Expected Behavior:
The readStream or bytes properties should contain the file content.

Actual Behavior:
Both readStream and bytes are null.

Additional Information:

  • Flutter version: 3.10.6
  • FilePicker plugin version: 6.0.0
  • Platform (iOS/Android): Android

Code snippet

Future<void> _pickCSVFile() async {
    try {
      print('Attempting to pick CSV file.');
      FilePickerResult? result = await FilePicker.platform.pickFiles(
        type: FileType.custom,
        allowedExtensions: ['csv'],
      );

      if (result != null) {
        print('CSV file picked successfully.');

        // Ensure that the file content is read into memory
        PlatformFile file = result.files.first;

        await readFileContents(file);
      } else {
        print('No files selected.');
      }
    } catch (e) {
      print('Error picking CSV file: $e');
    }
  }

// Add a new function for reading file contents
  Future<void> readFileContents(PlatformFile file) async {
    try {
      List<int> fileBytes = [];

      if (file.readStream != null) {
        // Read the content of the file asynchronously using ByteStream
        await for (List<int> bytesChunk in file.readStream!) {
          fileBytes.addAll(bytesChunk);
        }
      } else if (file.bytes != null) {
        // Use file.bytes directly if readStream is null
        fileBytes = file.bytes!;
      } else {
        print('Error: Both readStream and bytes are null.');
        return;
      }

      if (fileBytes.isNotEmpty) {
        setState(() {
          _selectedFile = PlatformFile(
            path: file.path,
            name: file.name,
            bytes: Uint8List.fromList(fileBytes),
            readStream: file.readStream,
            size: file.size,
          );
          _fileController.text = _selectedFile!.name;
        });

        // Add debug print for file content
        print('File Content: ${_selectedFile?.bytes}');
      } else {
        print('Error: File bytes is empty.');
      }
    } catch (e) {
      print('Error reading file content: $e');
    }
  }

```dart

'''
FilePickerResult? result = await FilePicker.platform.pickFiles(
type: FileType.custom,
withData: true,
allowedExtensions: ['csv'],
);
'''
This worked for me.