Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/angularjs/20.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Flutter 使用文件选择器在颤振中将平台文件制成文件_Flutter_Firebase Storage_Flutter Dependencies_Filepicker - Fatal编程技术网

Flutter 使用文件选择器在颤振中将平台文件制成文件

Flutter 使用文件选择器在颤振中将平台文件制成文件,flutter,firebase-storage,flutter-dependencies,filepicker,Flutter,Firebase Storage,Flutter Dependencies,Filepicker,我正在使用文件选择器插件从设备中选择文件。该文件是在平台文件的数据类型中选择的,但我想将该文件发送到Firebase存储,我需要一个常规文件。如何将平台文件转换为文件,以便将其发送到Firebase存储?代码如下: PlatformFile pdf; final GlobalKey<FormState> _formKey = GlobalKey<FormState>(); void _trySubmit() async { final isValid = _fo

我正在使用文件选择器插件从设备中选择文件。该文件是在平台文件的数据类型中选择的,但我想将该文件发送到Firebase存储,我需要一个常规文件。如何将平台文件转换为文件,以便将其发送到Firebase存储?代码如下:

PlatformFile pdf;
final GlobalKey<FormState> _formKey = GlobalKey<FormState>();

void _trySubmit() async {
    final isValid = _formKey.currentState.validate();
    if (isValid) {
      _formKey.currentState.save();
      final ref = FirebaseStorage.instance
          .ref()
          .child('article_pdf')
          .child(title + '-' + author + '.pdf');
      await ref.putFile(pdf).onComplete; // This throws an error saying that The argument type 'PlatformFile' can't be assigned to the parameter type 'File'
    }
  }

void _pickFile() async {
    FilePickerResult result = await FilePicker.platform.pickFiles(
      type: FileType.custom,
      allowedExtensions: ['pdf'],
    );
    if (result != null) {
      pdf = result.files.first;
    }
  }
PlatformFile pdf;
最终的GlobalKey _formKey=GlobalKey();
void\u trySubmit()异步{
final isValid=_formKey.currentState.validate();
如果(有效){
_formKey.currentState.save();
final ref=FirebaseStorage.instance
.ref()
.child('article_pdf')
.child(title+'-'+author+'.pdf');
wait ref.putFile(pdf).onComplete;//这会引发一个错误,说明无法将参数类型“PlatformFile”分配给参数类型“File”
}
}
void\u pickFile()异步{
FilePickerResult结果=等待FilePicker.platform.pickFiles(
类型:FileType.custom,
允许的扩展:['pdf'],
);
如果(结果!=null){
pdf=result.files.first;
}
}
试试这个:

PlatformFile pdf;
final File fileForFirebase = File(pdf.path);

快乐编码!:)

如果您使用的是web应用程序,您可以使用Flatter_file_picker将图像文件发布到Firestore:(摘自常见问题页面):


这在颤振Web上不起作用,因为
平台文件。Web上没有路径
。颤振Web的路径不可用working@lenz-也许,我只在移动设备上使用过它(Androis和IOS)。@RobertRobinson如果它在网络上工作,你必须添加路径、名称和大小,我想。
// get file
final result = await FilePicker.platform.pickFiles(type: FileType.any, allowMultiple: 
false);

if (result.files.first != null){
  var fileBytes = result.files.first.bytes;
  var fileName = result.files.first.name;

  // upload file
  await FirebaseStorage.instance.ref('uploads/$fileName').putData(fileBytes);
}