添加链接
link管理
链接快照平台
  • 输入网页链接,自动生成快照
  • 标签化管理网页链接
相关文章推荐
幸福的红酒  ·  css 宽度 ...·  2 年前    · 
玩命的葫芦  ·  python ...·  2 年前    · 
Collectives™ on Stack Overflow

Find centralized, trusted content and collaborate around the technologies you use most.

Learn more about Collectives

Teams

Q&A for work

Connect and share knowledge within a single location that is structured and easy to search.

Learn more about Teams

I want to convert an XFile to File to upload the file to Firebase since Firebase only uploads in File format. The code is as follows:

XFile videofile;
videofile = file;
await FirebaseStorage.instance.ref(imageRef).putFile(videoFile);

Gives an error saying XFile can't be uploaded to Firebase

Using XFile package from XFile package gives another error saying:

The name 'XFile' is defined in the libraries 'package:cross_file/src/types/interface.dart' and 'package:xfile/src/xfile_core.dart (via package:xfile/xfile.dart)'. Try using 'as prefix' for one of the import directives, or hiding the name from all but one of the imports.

other dependencies in the file are

import 'dart:async';
import 'dart:io';
import 'package:firebase_storage/firebase_storage.dart';
import 'package:firebase_auth/firebase_auth.dart';
import 'package:camera/camera.dart';
import 'package:flutter/material.dart';
import 'package:video_player/video_player.dart';
                Just in case someone else encounter this. I accidentally imported from 'dart:html', should import from 'dart:io' instead.
– Victor Kwok
                Dec 9, 2021 at 5:15

To convert your Xfile image to File image to show the image inside the widget you can use:

XFile? selectedImage;
Image.file(File(selectedImage!.path))

For anyone facing challenges with the network-type path returned by xFile.path, which cannot be used to create a file, you can instead use the FireStore putData function and read in the xFile as bytes.

FirebaseStorage store = FirebaseStorage.instance;
final _picker = ImagePicker();
XFile? pickedImage = await _picker.pickImage(source: ImageSource.gallery);
    if (pickedImage != null) {
      TaskSnapshot task = await store.ref(imageRef).putData(await pickedImage.readAsBytes());

It's always a good idea to compress images where necessary before handling/processing.

var _image34 = await ImagePicker().pickImage(source: ImageSource.camera, maxWidth: 200.0, maxHeight: 200.0);
FirebaseStorage fs = FirebaseStorage.instance;
Reference  rootReference = fs.ref();
Reference pictureFolderRef = rootReference.child("pictures").child("image");
pictureFolderRef.putFile(File(_image34!.path)).whenComplete(() => print("uploaded")).then((storageTask) async {
  String link = await storageTask.ref.getDownloadURL();

Hello, here is the answer for those who have updated libraries today (08/26/2021).

XFile videofile;
videofile = file;
await FirebaseStorage.instance.ref(imageRef).putFile(File(videoFile!path));

Please notify if it helped, thank you.

Thanks for contributing an answer to Stack Overflow!

  • Please be sure to answer the question. Provide details and share your research!

But avoid

  • Asking for help, clarification, or responding to other answers.
  • Making statements based on opinion; back them up with references or personal experience.

To learn more, see our tips on writing great answers.