如何将图像从firebase存储下载到当前用户Cloud Firestore Flatter?

如何将图像从firebase存储下载到当前用户Cloud Firestore Flatter?,firebase,flutter,google-cloud-firestore,firebase-storage,Firebase,Flutter,Google Cloud Firestore,Firebase Storage,这是用于将所选图像从图像选择器保存到firebase存储的代码 Future uploadPic(BuildContext context) async { String fileName = basename(_image.path); StorageReference firebaseStorageRef = FirebaseStorage.instance.ref().child(fileName); StorageUploadTask upload

这是用于将所选图像从图像选择器保存到firebase存储的代码

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;
    setState(() {
      print("Profile Picture uploaded");
      Scaffold.of(context)
          .showSnackBar(SnackBar(content: Text('Profile Picture Uploaded')));
    });
  }
在选择一个图像作为经过身份验证的用户(我已经负责了身份验证)时,如何将该图像转换为当前用户云firestore的URL,该图像可以显示在应用程序的不同区域

我通过当前用户的uid保存用户信息,以便进一步上下文:

Future<void> userSetup(String displayName) async {
  int plastics = 0;
  final CollectionReference users =
      FirebaseFirestore.instance.collection('Users');
  FirebaseAuth auth = FirebaseAuth.instance;
  String uid = auth.currentUser.uid.toString();
  users.doc(uid).set({'displayName': displayName, 'uid': uid});
  users.doc(uid).update({'plastics': plastics});
  return;
}
未来用户设置(字符串显示名称)异步{
int=0;
最终收集参考用户=
FirebaseFirestore.instance.collection('Users');
FirebaseAuth auth=FirebaseAuth.instance;
字符串uid=auth.currentUser.uid.toString();
users.doc(uid).set({'displayName':displayName,'uid':uid});
users.doc(uid.update({'plastics':plastics});
返回;
}

以社区维基的形式发布,基于评论

您可以使用下面的代码(根据您的需要进行调整,基于此答案)将图像保存到Firestore中

Future Build() async {
    String fileName = basename(_image.path);
    StorageReference reference = storage.ref().child('image');
    StorageUploadTask uploadTask = reference.putFile(fileName);
    StorageTaskSnapshot taskSnapshot = await uploadTask.onComplete;
    String url = await taskSnapshot.ref.getDownloadURL();
    imageUrl1 = url;
    return url;
}

使用这种方式编写的函数,您应该能够将图像从特定路径保存到数据库中。除此之外,请记住,此代码应该是您的起点,因为考虑到变量名称和位置,可能需要进行更改。

此处提供了解决方案