shangod
shangod

Reputation: 667

How to convert a XFile to File in Flutter

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

Upvotes: 56

Views: 82532

Answers (7)

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

Upvotes: 0

IkayU
IkayU

Reputation: 51

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.

Upvotes: 5

botCoder
botCoder

Reputation: 375

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

Upvotes: 30

Shahzaib Ahmed
Shahzaib Ahmed

Reputation: 329

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

Upvotes: 0

Douglas
Douglas

Reputation: 39

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.

Upvotes: -2

yasar can clngr
yasar can clngr

Reputation: 1206

File file = File(videofile.path);

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

Upvotes: 119

fartem
fartem

Reputation: 2531

final file = videofile.toFile();

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

Upvotes: 1

Related Questions