Cannot retrieve length of file, path = 'null' (OS Error: No such file or directory, errno = 2)

3.2k Views Asked by At

I'm new migrating to flutter newest version. I've been migrating Flutter v1 to v3, but when I use the plugin file_picker: ^5.0.1 gallery and camera are working fine, but whenever is an file, this error appears, I've been searching everywhere and I haven't found a solution for this, does anyone know how I can fix this problem?

 var request = http.MultipartRequest('POST', Uri.parse(baseURL! + urlFiles));
request.fields['apiKey'] = API_KEY;
request.fields['username'] = username as String;
request.fields['authenticationCode'] = authCode as String;
request.files.add(await http.MultipartFile.fromPath('myfile', filename)); //<-- Error

var streamedResponse = await request.send();
final response = await http.Response.fromStream(streamedResponse);

// If the response from the server is correct code == 200 we proceed with the parsing of the json
if (response.statusCode == 200) {
  final responseJson = json.decode(response.body);

This is how I've been working files and images:

  _pickFile() async {

FilePickerResult? result = await FilePicker.platform.pickFiles();
if (result == null) return;

PlatformFile file = result.files.first;

print('File Name: ${file.name}');
print('File Size: ${file.size}');
print('File Extension: ${file.extension}');
print('File Path: ${file.path}');
//   if (path == null) return;
Navigator.pop(context);
_uploadFile('$path');}

_getPhoto(ImageSource source) async {
    var image = await ImagePicker().getImage(source: source, imageQuality: 50);

    if (image == null) return;

    path = image.path;
    Navigator.pop(context);
    if (kDebugMode) {
      print('---> ImagePicker $path');
    }
    _uploadFile(path as String);}
2

There are 2 best solutions below

0
Ashley On BEST ANSWER

At the end I solved it by adding these new lines and everything is working as expected and it's received by the server.

  _pickFile() async {
final path = await FilePicker.platform.pickFiles(allowMultiple: true);
String? filePath = path?.files.single.path;

if (path == null) return;
Navigator.pop(context);
_uploadFile('$filePath');
}
0
Maha Mamdouh On

don't save the full path for files "save only folder and file name" and each time want to get access on it, get documents directory and append to it folder and file name as path component.