0

Getting below error while using the latest version of cloud_firestore: ^0.14.0+2 while uploading image on cloud firestore.

Uncaught (in promise) Error: [cloud_firestore/unknown] Invalid argument (dartObject): Could not convert: Instance of '_Uri'

The image gets successfully uploaded in the storage portion but the image link doesn't get updated in cloud firestore db.

Below is the class from where I pick the image and and hit he upload btn to upload the image

class AddNewItemView extends StatefulWidget {
  @override
  _AddNewItemViewState createState() => _AddNewItemViewState();
}

class _AddNewItemViewState extends State<AddNewItemView> {
  MediaInfo _itemPic; 
 @override
  Widget build(BuildContext context) {
    return Scaffold(
    .....
    child: RawMaterialButton(
          onPressed: () async {
              MediaInfo pickedImg =  await ImagePickerWeb.getImageInfo; <---- image_picker_web package
               setState(() {
                 _itemPic = pickedImg;
                });
    .........
    FlatButton(
         onPressed: () async {
            bool addDataSuccess = await model.addNewItem( _itemPic);
            if (addDataSuccess) {
                 print("Inside addDataSuccess");
            } else {
                 print("Outside addDataSuccess");
                   }
                 },
              ),
    .....
    );
}

This class is responsible for extracting image uri from uploadImageFile funtion and uploading it in cloud firestore. Any other info passed alongside pic gets uploaded in cloud firestore, only 'pic' is not uploading

class ItemViewModel extends BaseViewModel {
           Future<bool> addNewItem(   MediaInfo pic ) async {
         
                 Uri _uploadedImgURL = await ConstantFtns()
                     .uploadImageFile(pic, "ImgPathString", "fileName");


     await  FirebaseFirestore.instance.collection('item').doc("tempID").set(
        {
              'pic': _uploadedImgURL,
          'otherInfo': 'otherTempInfo' 
    },
        );
}

in below class I get valid imageUri without any error and image gets uploaded in storage portion only problem is that it don't get uploaded in cloud firestore database

import 'package:firebase/firebase.dart' as fb;
import 'package:mime_type/mime_type.dart';
import 'package:path/path.dart' as p;

class ConstantFtns  {
  Future<Uri> uploadImageFile(
      MediaInfo mediaInfo, String ref, String fileName) async {
    try {
      String mimeType = mime(p.basename(mediaInfo.fileName));
       final String extension = extensionFromMime(mimeType);

      var metadata = fb.UploadMetadata(
        contentType: mimeType,
      );

      fb.StorageReference storageReference =
          fb.storage().ref(ref).child(fileName + ".$extension");

      fb.UploadTaskSnapshot uploadTaskSnapshot =
          await storageReference.put(mediaInfo.data, metadata).future;

      Uri imageUri = await uploadTaskSnapshot.ref.getDownloadURL();
      print("download url $imageUri");  <---- IT GETS P[RINTED AND IT SHOWED VALED IMAGE URL STORED IN STORAGE
      return imageUri;
    } catch (e) {
      print("File Upload Error $e");
      return null;
    }
  }
}

enter image description here

Faizan Kamal
  • 1,732
  • 3
  • 27
  • 56

1 Answers1

0

Since what you want is simply to store the URL into firestore you don't really need the getDownloadURL() value to be a Uri object, you just need it's string value, being the error you are getting a conversion error, I suggest that you follow what is recommended in this community answer, that way your code will look like this:

String uploadImageFile(
    MediaInfo mediaInfo, String ref, String fileName) async {
        try {
            ...

            var imageUri = await uploadTaskSnapshot.ref.getDownloadURL() as String;
            print("download url $imageUri");  <---- IT GETS P[RINTED AND IT SHOWED VALED IMAGE URL STORED IN STORAGE
            return imageUri
        } catch (e) {
            ...
        }
    }
})

And you also have to change the call in the ItemViewModel class to:

String _uploadedImgURL = await ConstantFtns()
                 .uploadImageFile(pic, "ImgPathString", "fileName");
Ralemos
  • 5,571
  • 2
  • 9
  • 18