firebase - 上传用户个人资料图片 Firebase - Flutter

标签 firebase flutter dart firebase-storage

我正在使用 ImagePicker 选择个人资料图片,一旦选择图片,它将被分配给 Circle Avatar 现在我如何将图片上传到 firebase。如果用户刷新页面 (UI) Circle Avatar 将返回到默认图像!我需要用当前用户的图片替换 Circle Avatar

只是我不知道如何从 firebase 检索图像并将其分配到 Circle Avatar

这是我尝试更新圆圈头像的方式 -

 CircleAvatar(
                            radius: 30.0,
                            backgroundColor: Colors.white,
                            child: (_imageFile != null)
                                ? Container(
                                    decoration: BoxDecoration(
                                      shape: BoxShape.circle,
                                      image: DecorationImage(
                                          image: FileImage(_imageFile),//Selected Image
                                          fit: BoxFit.fill),
                                    ),
                                  )
                                : Image.network(
                                    (photoUrl == null)
                                        ? 'https://www.materialui.co/materialIcons/image/add_a_photo_black_36x36.png'//Default Picture
                                        : photoUrl,
                                    fit: BoxFit.fill,
                                  ), //Replace with Image From DB

上传图片-

Future UpdatePic() async{
   String fileName = basename(_imageFile.path);
  StorageReference firebaseStorageRef =
      FirebaseStorage.instance.ref().child(fileName);
  StorageUploadTask uploadTask = firebaseStorageRef.putFile(_imageFile);
  StorageTaskSnapshot taskSnapshot = await uploadTask.onComplete;
  showInSnackBar("Picture updated successfully.");
}

这会上传图片,但我不知道如何将其检索到该特定用户个人资料的 Circle Avatar 中。

最佳答案

首先您需要获取您存储的照片的下载地址:

FirebaseStorage.instance
          .ref() //if your photos are inside another folder, lets say avatarPhotos
                 // you need to put that here also to get the download URL, with
                 // another .child('avatarPhotos') 
          .child(fileName)
          .getDownloadURL()
          .then((value) => {
                 //here you can either set your photoUrl to value or assuming
                 //assuming you have a database with users with profile pictures, 
                 //you can call a method to update the user with this value
                 photoUrl = value;
              });
    });

然后您可以将下载 URL 存储在一个变量中,并像您在代码中使用 Image.network(photoUrl) 那样输出它。

关于firebase - 上传用户个人资料图片 Firebase - Flutter,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62876943/

相关文章:

firebase - 我应该将 firebase api key 隐藏到后端吗?不是因为数据安全,而是项目克隆问题

dart - Flutter Cards - 如何在 Flutter 中循环卡片小部件

flutter - 如何使用 ref.listen 对 Riverpod FutureProvider 使用react?

if-statement - 在Dart中如何使用true的返回值?

string - 在dart中获取String的前几个单词

arrays - 在 Firebase 数据库中存储数组

firebase - 如何使用 github 操作和 w9jds/firebase-action 部署到不同的 firebase 环境

flutter - Flutter Application 中的 Ephemeral state 和 App state 是什么意思?

android - Firebase 验证码检查失败并阻止用户进行身份验证

flutter - 具有多个列和const宽度/高度卡的Listview