How to get Download URL from Firebase Storage in flutter
1,217
Solution 1
Hope You're Doing Well … You Can Try This Method To Get The URL Of The Image(Any File) From Firebase Storage To Firebase Store And Then You Can Retrieve Image .
class _UploadAdState extends State<UploadAdPage> {
final formKey = GlobalKey<FormState>();
File _myimage;
String imgUrl;
Future getImage1(File chosenimage) async {
PickedFile img =
await ImagePicker.platform.pickImage(source: ImageSource.gallery);
if (chosenimage == null) return null;
File selected = File(img.path);
setState(() {
_myimage = chosenimage;
});
}
// changing the firestore rules and deleteing if request.auth != null;
sendData() async {
// to upload the image to firebase storage
var storageimage = FirebaseStorage.instance.ref().child(_myimage.path);
UploadTask task1 = storageimage.putFile(_myimage);
// to get the url of the image from firebase storage
imgUrl1 = await (await task1).ref.getDownloadURL();
// you can save the url as a text in you firebase store collection now
}
}
Solution 2
I am using in my app this function. Pass image file and download with getDownloadUrl .
Future <String> _uploadphotofile(mFileImage) async {
final Reference storageReference = FirebaseStorage.instance.ref().child("products");
UploadTask uploadTask = storageReference.child("product_$productId.jpg").putFile(imgfile);
String url = await (await uploadTask).ref.getDownloadURL();
return url;
}
Author by
Jeevan Crasta
Updated on December 29, 2022Comments
-
Jeevan Crasta over 1 year
The Following Code is used to Upload any image from gallery/Camera to Firebase storage. I was successful in uploading the image to storage along with meta data. Now the problem is I am not able to get the download URL of the uploaded image. Tried a lot but didn't find any solution.
FirebaseStorage storage = FirebaseStorage.instance; final picker = ImagePicker(); PickedFile pickedImage; File imageFile; Future<void> _upload(String inputSource) async { try { pickedImage = await picker.getImage( source: inputSource == 'camera' ? ImageSource.camera : ImageSource.gallery, maxWidth: 1920); final String fileName = path.basename(pickedImage.path); imageFile = File(pickedImage.path); try { // Uploading the selected image with some custom meta data await storage.ref(fileName).putFile( imageFile, SettableMetadata( customMetadata: { 'uploaded_by': 'A bad guy', 'description': 'Some description...' }, ), ); // Refresh the UI setState(() {}); } on FirebaseException catch (error) { print(error); } } catch (err) { print(err); } }