Flutter正确地从Firebase获取数据

时间:2020-09-25 11:26:20

标签: firebase flutter google-cloud-firestore

嘿,在我的应用中,您可以编辑个人资料,例如从图库中选择一张图片作为个人资料图片。当我在应用程序中时,一切正常。但是,当我关闭应用程序并再次打开它时,个人资料照片消失了。当我从图库中选择图片时,该网址会存储在Cloud Firebase中。在我的代码中,我从Cloud Firebase检索了URL,并希望这个Constants.URL_Profil_Picture = value.docs [0] .data()['url'];是工作。但它不起作用。有人知道为什么吗?

 final FirebaseFirestore _fireStore = FirebaseFirestore.instance;
  getData() async {
    return await _fireStore.collection("SerX").get();
  }

  Future uploadPic(BuildContext context) async {
    String fileName = basename(_image.path);
    StorageReference firebaseStorageRef = FirebaseStorage.instance.ref().child(
        fileName);
    StorageUploadTask uploadTask = firebaseStorageRef.putFile(_image);
    StorageTaskSnapshot taskSnapshot = await uploadTask.onComplete;
    var downUrl = await (await uploadTask.onComplete).ref.getDownloadURL();
    _URL = downUrl.toString();


    setState(() async {
      print('Profile Picture uploaded');
      var firebaseUser = await FirebaseAuth.instance.currentUser;
      FirebaseFirestore.instance.collection("SerX").doc(firebaseUser.uid).update({"url" : downUrl});

      getData().then((value){
        if(value.docs.length > 0){
          Constants.URL_Profil_Picture = value.docs[0].data()['url'];
         print(value.docs[0].data()['url']);
        }
      });
      print(Constants.URL_Profil_Picture);
      Scaffold.of(context).showSnackBar(
          SnackBar(content: Text('Profile Picture Uploaded')));
    });
  }

0 个答案:

没有答案