繁体   English   中英

如何在 Firebase 存储(颤振)中的文件更改时重建个人资料图片小部件

[英]How to rebuild profile picture widget on changes to file in Firebase Storage (Flutter)

我正在尝试在 Flutter 中构建一个个人资料图片小部件,允许用户将图像上传到 Firebase 存储并在上传后检索该图像。 我的问题是我在 initState() 中调用了 _getImage() Function 所以只要用户上传新照片,除非重新启动应用程序,否则小部件不会用新照片重建。 是否有类似“流”的方法可以用来监听 Firebase 存储位置的变化并相应地重建小部件?

我使用 ImagePicker 从图库中获取我的图像。

_pickImage() async {
final selected = await ImagePicker().getImage(source: ImageSource.gallery);
setState(() {
  _imageFile = File(selected.path);
});
_uploadFile(_imageFile);}

选择照片后,将其上传到 Firebase 存储。

_uploadFile(File _imageFile) async{
  var user = await FirebaseAuth.instance.currentUser();
  var storageRef = _storage.ref().child('user/profile/${user.uid}');
  var uploadTask = storageRef.putFile(_imageFile);
}

然后为了检索图像,我在 initState() 中调用 _getImage() function。

void initState() {
  super.initState();
  _getImage();

}

_getImage() 函数。

_getImage() async {
var user  = await FirebaseAuth.instance.currentUser();
var photoReference = _storage.ref().child("user/profile/${user.uid}");
photoReference.getData(1000000000).then((data){
  this.setState(() {
    imageFile = data;
  });
}).catchError((onError){
  print('error');
}); }

这是一个例子

final image = await ImagePicker()
                  

.pickImage(source:  ImageSource.gallery);

                  if (image == null) return;
              FirebaseStorage storage = FirebaseStorage.instance;
              Reference ref = storage.ref().child("images/${DateTime.now()}");
              UploadTask uploadTask = ref.putFile(File(image.path));
              uploadTask.then((res) async {
              String fileurl = await (await uploadTask).ref.getDownloadURL();
              return fileurl;

您必须在 firebase 上上传图像,然后检索图像 url 然后您可以在用户配置文件中调用它

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM