【问题标题】:Flutter Web: File Picker throws 'Invalid argument(s) (path): Must not be null' errorFlutter Web:文件选择器抛出“无效参数(路径):不能为空”错误
【发布时间】:2021-04-01 20:37:07
【问题描述】:

目标:使用文件资源管理器选择文件并上传到 Firebase 存储。

包:file_picker:^2.1.4

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

文件资源管理器可以正常打开,我可以选择一个文件。但是,在我选择文件后没有任何反应。以下是我到目前为止尝试过的代码:

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(result.files.single.path);
  • result 对象是否完全为空?可以详细介绍一下吗?
  • @easeccy:我已经更新了这个问题。希望这可以帮助。谢谢!

标签: flutter flutter-web


【解决方案1】:

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

显然,path 在使用 web 时始终是 null,根据 file_picker wiki。 他们建议改用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.putData(uploadfile);代替storageRef.putFile(uploadfile);

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

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

【讨论】:

  • 非常感谢!这已经给我带来了几天的麻烦!我只收到控制台错误:“无法读取 null 的属性 'Symbol(dartx.contains)'”
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2022-08-15
  • 2019-01-25
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-11-09
  • 1970-01-01
相关资源
最近更新 更多