如何在Flutter中获取数据库中图像的下载URL [英] How do i get the image download URL in database in Flutter

查看:283
本文介绍了如何在Flutter中获取数据库中图像的下载URL的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想获取图像下载链接并将其存储在 Firestore 数据库中。 Bellow是用于将图像上传到Firebase存储的函数,如果该函数有误,您可以解释原因并为我编写一个函数吗?

I want to get the image download link and store it in Firestore database. Bellow is the function to upload the image to Firebase storage, if the function is wrong can you please explain why and write me a function?

File _image1;
Future uploadPic() async {
    StorageReference reference = storage.ref().child('image');
    StorageUploadTask uploadTask = reference.putFile(_image1);
    StorageTaskSnapshot taskSnapshot = await uploadTask.onComplete;
    String url = await taskSnapshot.ref.getDownloadURL();
    imageUrl1 = url;
  }


推荐答案

add返回到函数检索输出(URL)

add return to the function to retrieve the output(url)

File _image1;
Future uploadPic() async {
    StorageReference reference = storage.ref().child('image');
    StorageUploadTask uploadTask = reference.putFile(_image1);
    StorageTaskSnapshot taskSnapshot = await uploadTask.onComplete;
    String url = await taskSnapshot.ref.getDownloadURL();
    imageUrl1 = url;
    return url;
  }

由此您可以调用then函数,

From this you can call the then function,

uploadPic().then((value)
            {
        DocumentReference docRef = 
        Firestore.instance.collection('collectionname').document(documentid);
             docRef.setData({'pic' : value},merge:true)
             });

这篇关于如何在Flutter中获取数据库中图像的下载URL的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆