Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to convert a XFile to File in Flutter

Tags:

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'; 
like image 286
shangod Avatar asked Feb 13 '21 13:02

shangod


People also ask

How do I convert a list of XFile to a File in flutter?

XFile xfile = ...; File file = File(xfile. path); If you have a list, you can use map or other way you prefer to create File using the path of a XFile .

How do I convert PlatformFile to flutter?

Try this: PlatformFile pdf; final File fileForFirebase = File(pdf. path);

What is XFile Dart?

XFile class Null safetyA CrossFile is a cross-platform, simplified File abstraction. It wraps the bytes of a selected file, and its (platform-dependant) path.


2 Answers

File file = File(videofile.path); 

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

like image 145
yasar can clngr Avatar answered Oct 08 '22 02:10

yasar can clngr


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)) 
like image 40
botCoder Avatar answered Oct 08 '22 03:10

botCoder