【发布时间】: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