颤振网络:文件选取器抛出'Invalid argument(s) (path): Must not be null'错误



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

文件包:file_picker:^2.1.4

问题:抛出错误:";无效参数(路径(:不能为null;。

文件资源管理器打开良好,我可以选择一个文件。但是,在我选择一个文件之后,什么都不会发生。以下是我迄今为止尝试过的代码:

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);

我尝试过多个博客中提供的各种建议。即使这里的解决方案也无济于事,我也会得到同样的错误。参见以下代码:

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,在使用web时,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问题的人。

最新更新