pirekare
pirekare

Reputation: 171

About uploading images to Firebase Storage (with image_picker 0.8.3+1)

After the update in Image_Picker, "XFile" is used instead of "File" and "pickImage(...)" is used instead of "getImage(...)".

await _picker.pickImage(source: ImageSource.camera)

this code now returns XFile.

Related codes are below

    XFile _image;
    final ImagePicker _picker = ImagePicker();

    Future getImageFromCamera() async {
      final pickedFile = await _picker.pickImage(source: ImageSource.camera);

      setState(() {
        if (pickedFile != null) {
          _image = pickedFile;
        } else {
          print("No image selected");
        }
      });
    }

    Future<void> uploadImageToStorage(XFile imageFile) async {
      String imageName = "${AuthService().getCurrentUID()}";

      FirebaseStorage.instance
          .ref()
          .child("photos")
          .child(imageName)
          .putFile(_image);
    }

error

The argument type 'XFile' can't be assigned to the parameter type 'File'. 

I can't write "_image" in putFile(...). It asks me for "File" type. But _image is of type "XFile". How can I upload an image into Firebase Storage?

Upvotes: 1

Views: 770

Answers (1)

Huthaifa Muayyad
Huthaifa Muayyad

Reputation: 12383

import 'dart:io' as i;

Future<void> uploadImageToStorage(XFile imageFile) async {
      String imageName = "${AuthService().getCurrentUID()}";

      FirebaseStorage.instance
          .ref()
          .child("photos")
          .child(imageName)
          .putFile(i.File(_image.path));
    }

You need to convert it into a file object using File from the XFile's path.

Upvotes: 1

Related Questions