【发布时间】:2020-12-10 14:40:31
【问题描述】:
我想从我的 Firebase 存储中加载一张图片。我为名为 {users uid}.png 的用户上传了图片,例如个人资料照片。因此,当我进入用户个人资料屏幕时,我想将这些图片从 firebase 上传到当前用户 uid。最好的方法是什么?我有一个异步方法来设置我的用户属性,例如final loggegInUser,我有一个异步方法
void getCurrentUser() async {
try {
final user = await _auth.currentUser();
if (user != null) {
loggedInUser = user;
print(loggedInUser.email);
print(uid);
}
} catch (e) {
print(e);
}
}
这个方法设置我的全局loggedInUser属性然后我想像这样从firebase存储加载图像
CircleAvatar(
backgroundColor: Colors.black,
backgroundImage: FirebaseImage(
'gs://homeparty-68792.appspot.com/user_profile_images/${loggedInUser.uid}.png')
,
radius: 100,
),
但是当我加载这个屏幕时,我得到了
ERROR TYPE Exception has occurred. NoSuchMethodError (NoSuchMethodError: The getter 'uid' was called on null. Receiver: null Tried calling: uid)
错误。 getCurrentUser() 方法正常工作,它打印电子邮件和密码,但在构建小部件中它返回 null。为什么会发生这种情况我需要一些帮助???
【问题讨论】:
-
使用
bloc。将getCurrentUser放入initState。 -
您没有展示代码,但很可能您还不了解期货如何工作或如何根据未来结果构建小部件。
-
这能回答你的问题吗? What is a Future and how do I use it?
-
你在哪里打电话
getCurrentUser()? -
我在上面发布的链接解释了如何正确使用期货并等待它们。或者,您在下面得到的答案为您提供了一个简单的出路,因为您将能够同步获得结果。但无论如何,你迟早会需要这个。
标签: firebase flutter dart firebase-authentication