flutterurluriimagepickerflutter-image-picker

How to convert image Xfile to URL in flutter?


in my flutter App, I am using ana Image picker, now I want to update the current user photoUrl(Firebase auth) but I don't know how to convert a file to Url?

Any idea??

final XFile? image = await ImagePicker().pickImage(source: ImageSource.gallery);


Solution

  • You can use firebase Storage to upload an Image on Firebase and get ImageUrl

    Use this dependency: firebase_storage: ^11.0.10

    void uploadImage(String value) async {
    if (await Utils.hasNetwork()) {
      Utils.showLoader();
      File image = File(value);
      FirebaseStorage storage = FirebaseStorage.instance;
      Reference ref =
          storage.ref().child("${SharedPreferenceHelper().getUserId()}");
      UploadTask uploadTask = ref.putFile(image);
      uploadTask.then((res) {
        if (res.state == TaskState.success) {
          res.ref.getDownloadURL().then((url) {
           print(url);
          }).catchError((onError) {
            print("Got Error $onError");
          });
        }
      });
    }
    

    }