58

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

7 Answers 7

123
File file = File(videofile.path);

.toFile() may not work because XFile plugin may conflict with another plugins

Sign up to request clarification or add additional context in comments.

2 Comments

Just in case someone else encounter this. I accidentally imported from 'dart:html', should import from 'dart:io' instead.
This will not work for Flutter web because we can't use dart:io's File, so, we need to use XFile.
30

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

Comments

5

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.

1 Comment

But this will load the file into memory first, which is not an ideal solution for large files. We need to use putFile for the Cloud Storage SDK to read it asynchronously and upload data, instead of reading the whole file into memory first, then uploading it as a single chunk of data.
1
final file = videofile.toFile();

You can find more examples in plugin's docs here.

Comments

1
File convertToFile(XFile xFile) => File(xFile.path);

Comments

0
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();
});

Comments

-2

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.

1 Comment

what kind of file would this store? jpg/png/gif?

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.