Flutter Web:文件选择器抛出 'Invalid argument(s) (path): Must not be null' 错误

Flutter Web: File Picker throws 'Invalid argument(s) (path): Must not be null' error

Objective:Select 使用文件资源管理器的文件并上传到 firebase 存储。

包:file_picker:^2.1.4

问题:抛出错误:“无效参数(路径):不得为空”。

文件资源管理器可以正常打开,我可以 select 一个文件。但是,在我 select 一个文件之后什么也没有发生。以下是我到目前为止尝试过的代码:

FilePickerResult result;
File uploadfile;

try{
    result = await FilePicker.platform.pickFiles(type: FileType.custom,
          allowedExtensions: ['jpg', 'pdf', 'doc', 'docx', 'xls', 'xlsx', 'txt'],
        );
} catch(e) { 
    print(e);
}

if(result != null) {
     
    try{
          uploadfile = File(result.files.single.path);
 
          String filename = basename(uploadfile.path);
 
          StorageReference storageRef = FirebaseStorage.instance.ref().child('$path$filename');
 
          final StorageUploadTask uploadTask = storageRef.putFile(uploadfile);
 
          final StorageTaskSnapshot downloadUrl = (await uploadTask.onComplete);
 
          if (downloadUrl.error == null){
 
            final String attchurl = (await downloadUrl.ref.getDownloadURL());
 
          }
    } catch(e) {
       print(e);
    }

我确定代码在以下位置抛出错误:uploadfile = File(result.files.single.path);

我尝试了多个博客中提供的各种建议。即使解决方案 here 也无济于事,我得到了同样的错误。请参阅下面的代码:

  FilePickerResult _filePickerResult;
  File uploadfile;
  try {
        _filePickerResult = await FilePicker.platform.pickFiles(
            type: FileType.custom,
           allowedExtensions: ['jpg', 'pdf', 'doc', 'docx', 'xls', 'xlsx', 'txt'],);
      } on PlatformException catch (e) {
        print("Unsupported operation" + e.toString());
      }

      if (_filePickerResult != null) {
        try{
          uploadfile = File(_filePickerResult.files.single.path);

          print(uploadfile);
        }catch(e){
          print(e);
        }
        
  }

任何帮助将不胜感激。谢谢!

*** 更新 ***

当我这样做时:

print(result);
print(result.files);
print(result.files.single);
print(result.files.single.name);
print(result.files.single.size);
print(result.files.single.path);

我得到:

Instance of 'FilePickerResult'
[Instance of 'PlatformFile']
Instance of 'PlatformFile'
FileName01.xlsx
10
null

所以本质上,result.files.single.path 失败了。希望这可以帮助。谢谢!

我可能已经解决了这个...

显然,根据 file_picker wiki,使用网络时 path 总是 null。 他们建议改用 bytes 来检索文件数据。

因此,按照上述说明和一些修改,我能够成功上传文件。修改后的代码现在看起来像这样:

FilePickerResult result;

  try{
    result = await FilePicker.platform.pickFiles(type: FileType.custom,
          allowedExtensions: ['jpg', 'pdf', 'doc', 'docx', 'xls', 'xlsx', 'txt'],
        );
  } catch(e)
  { print(e);
    }

      if(result != null) {
        try{

          Uint8List uploadfile = result.files.single.bytes;
        
          String filename = basename(result.files.single.name);
          
          fs.Reference storageRef = fs.FirebaseStorage.instance.ref().child('$dirpath$filename');
          
          final fs.UploadTask uploadTask = storageRef.putData(uploadfile);
          
          final fs.TaskSnapshot downloadUrl = await uploadTask;
          
          final String attchurl = (await downloadUrl.ref.getDownloadURL());
          
          await AttachmentService(orgid: orgID, orgname: orgName, projid: projID).addattachmentobjs(objType, objID, attchdate, filename, attchurl);
          
      }catch(e) {
          print(e);
        }

      }

基本上,我只是改变了:

FilePickerResult uploadfile = File(result.files.single.path);

至:

Uint8List uploadfile = result.files.single.bytes;

而不是 storageRef.putFile(uploadfile); ,我使用了 storageRef.putData(uploadfile);

我确实在 final fs.TaskSnapshot downloadUrl = await uploadTask; 遇到了 MissingPluginException No implementation found for method StorageReference#putData,我通过将 firebase_storage 插件更新到最新解决了这个问题。

希望这对以后遇到类似 Flutter Web 问题的人有所帮助。