2

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 ???

Peter Haddad
  • 78,874
  • 25
  • 140
  • 134
Furkan
  • 302
  • 3
  • 11
  • use `bloc`. Put `getCurrentUser` in `initState`. – John Joe Aug 21 '20 at 08:15
  • You did not show the code, but very likely you do not yet understand how futures work or how to build a widget based on a future result. – nvoigt Aug 21 '20 at 08:15
  • Does this answer your question? [What is a Future and how do I use it?](https://stackoverflow.com/questions/63017280/what-is-a-future-and-how-do-i-use-it) – nvoigt Aug 21 '20 at 08:15
  • Where are you calling `getCurrentUser()`? – Peter Haddad Aug 21 '20 at 08:16
  • I thought other parts of code does not effect the problem I just want to get current user uid and load an image accordingly.But you right I might have some confusions about how futures work. – Furkan Aug 21 '20 at 08:20
  • @PeterHaddad I called in state class – Furkan Aug 21 '20 at 08:22
  • @Xanubra Your problem is that your way to query the authenticated user is *asynchronous*, that means if you want the result, you have to *wait for it*. And you don't. You just rush to the widget building and your result is just not there yet when you build. The duplicate explains that. – nvoigt Aug 21 '20 at 08:24
  • @nvoig Yes I know I should wait that's why I' am asking a solution what is the proper way to doing it how can I wait ? until the image load. Thanks – Furkan Aug 21 '20 at 08:28
  • 1
    The link I posted above explains how to properly use futures and wait for them. Alternatively, the answer you got below gives you an easy way out , because you will be able to get your results synchronously. But sooner or later you will need this anyway. – nvoigt Aug 21 '20 at 08:53

1 Answers1

1

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'

Peter Haddad
  • 78,874
  • 25
  • 140
  • 134
  • I get 'Undefined name 'Firebase'. Try correcting the name to one that is defined, or defining the name' which package should I import I couldn't find . – Furkan Aug 29 '20 at 13:48
  • Did u add firebase_core? `firebase_core : ^0.5.0` – Peter Haddad Aug 29 '20 at 13:59
  • Yes I added firebase_core : ^0.5.0 in pubspec.yml – Furkan Aug 29 '20 at 14:10
  • When update my pubspec.yml as your suggestions I get more and more errors ex. I could not use FIrebaseUser anymroe – Furkan Aug 29 '20 at 14:14
  • Is there any way to wait until the getCurrentUser method complete ??? – Furkan Aug 29 '20 at 14:20
  • Have you read the answer and read the links? You don't have to wait anymore its synchronous – Peter Haddad Aug 29 '20 at 14:26
  • Ok i did everything as you said but still user.uid return null method. I create a User user property in State then isnide getCurrentUser i initialize it with _auth.currentUser then in build method I called user.uid. – Furkan Aug 29 '20 at 14:36
  • is the user loggedin? What do the print statement in the answer return? – Peter Haddad Aug 29 '20 at 14:38
  • OK i solved thank you soo much now my app working sync it prints the correct user uid. Hovewer bc of I uploaded my pubspec.yml now I can not use FirebaseImage class how can I get images from my firebase with current user uid without FirebaseImage ?? – Furkan Aug 29 '20 at 14:56
  • why cant u use it https://pub.dev/packages/firebase_image/example? – Peter Haddad Aug 29 '20 at 14:59
  • My fault I can use it so thank you so much for solution I really appreciate You think the question is useful you can vote it. – Furkan Aug 29 '20 at 15:03