I want to load a image from my Firebase storage. I uploaded images such as profile photos for users with the name {users uid}.png. So when I go to users profile screen, I want to upload these images from firebase accordingly to the current user uid. What is the best way for that?? I have a async method that sets my user properties such as final loggegInUser
and I have a async method
void getCurrentUser() async {
try {
final user = await _auth.currentUser();
if (user != null) {
loggedInUser = user;
print(loggedInUser.email);
print(uid);
}
} catch (e) {
print(e);
}
}
This method sets my global loggedInUser property then I want to load the image from firebase storage like that
CircleAvatar(
backgroundColor: Colors.black,
backgroundImage: FirebaseImage(
'gs://homeparty-68792.appspot.com/user_profile_images/${loggedInUser.uid}.png')
,
radius: 100,
),
But when I load this screen I get
ERROR TYPE Exception has occurred. NoSuchMethodError (NoSuchMethodError: The getter 'uid' was called on null. Receiver: null Tried calling: uid)
error. getCurrentUser() methods work properly it prints the e mail and password but in the build Widget It returns null. Why this is happening I need some help???
If you are calling getCurrentUser()
in initState
, then the problem is that the build()
is getting called before retrieving the user. Therefore the best thing to do is to upgrade cloud_firestore
to version 0.14.0
and to add 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
Then you can do the following, first initialize Firebase:
void main() async {
WidgetsFlutterBinding.ensureInitialized();
await Firebase.initializeApp();
runApp(MyApp());
}
and inside getCurrentUser()
:
void getCurrentUser() {
try {
final user = _auth.currentUser;
if (user != null) {
loggedInUser = user;
print(loggedInUser.email);
print(uid);
}
} catch (e) {
print(e);
}
}
In the new version, getting the currentUser
isn't asynchronous anymore doesnt require async/await
.
Some useful links:
No Firebase App '[DEFAULT]' has been created - call Firebase.initializeApp() in Flutter and Firebase
Undefined class 'FirebaseUser'
cloud_firestore 0.14.0 how to use the data method
The getter 'instance' isn't defined for the type 'Firestore'
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.