簡體   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