[英]Is there any way to load image from Firebase to flutter project according to current user UID
我想從我的 Firebase 存儲加載圖像。 我為名為 {users uid}.png 的用戶上傳了個人資料照片等圖片。 所以當我 go 到用戶配置文件屏幕時,我想將這些圖像從 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() 方法正常工作,它打印電子郵件和密碼,但在構建 Widget 中它返回 null。為什么會發生這種情況我需要一些幫助???
如果您在initState
中調用getCurrentUser()
,那么問題是在檢索用戶之前調用了build()
。 因此,最好的辦法是將cloud_firestore
升級到版本0.14.0
並添加 firebase_core firebase_core: 0.5.0
:
dependencies:
flutter:
sdk: flutter
firebase_core : ^0.5.0
firebase_auth : ^0.18.0
firebase_storage:^4.0.0
# cloud_firestore: ^0.14.0 not sure if you are using
然后可以進行如下操作,首先初始化Firebase:
void main() async {
WidgetsFlutterBinding.ensureInitialized();
await Firebase.initializeApp();
runApp(MyApp());
}
在getCurrentUser()
里面:
void getCurrentUser() {
try {
final user = _auth.currentUser;
if (user != null) {
loggedInUser = user;
print(loggedInUser.email);
print(uid);
}
} catch (e) {
print(e);
}
}
在新版本中,獲取currentUser
不再是異步的,不再需要async/await
。
一些有用的鏈接:
沒有創建 Firebase App '[DEFAULT]' - 在 Flutter 和 Firebase 調用 Firebase.initializeApp()
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.