1

I am pretty new to flutter. And I am getting this NoSuchMethodError: The getter 'uid' was called on null. I have red many posts and it seems like I need to use await with async. But I have no Idea how I can do it. This is happening when I want to pass the current uid of the user. Here is my code:

class ProfilePage extends StatefulWidget {
  //final String userProfileId;
  //ProfilePage({this.userProfileId});
  @override
  _ProfilePageState createState() => _ProfilePageState();
}

class _ProfilePageState extends State<ProfilePage> {

  FirebaseUser thecurrentuser;
  profilepicture() {

    return FutureBuilder(

        future: userReference.document(thecurrentuser.uid).get(), // So here I am getting the error
        builder: (context, dataSnapshot) {
        User user = User.fromDocument(dataSnapshot.data);
          if (!dataSnapshot.hasData) {
            return Padding(
            padding: EdgeInsets.all(17.0),
    }
          User user = User.fromDocument(dataSnapshot.data);
          return Padding(
            padding: EdgeInsets.all(17.0),
            child: Column(
              children: <Widget>[
                CircleAvatar(
                    radius: 45.0,
                    backgroundColor: Colors.grey,
                    backgroundImage: NetworkImage(
                        'https://upload.wikimedia.org/wikipedia/commons/9/9a/Mahesh_Babu_in_Spyder_%28cropped%29.jpg')),
                Text(user.username),
                Expanded(
                    flex: 1, child: Column(children: <Widget>[Text("coolbro")]))
              ],
            ),
          );
        });
  }

My user model

class User{
  final String uid;
  final String email;
  final String username;

  User({
    this.uid,
    this.email,
    this.username,
  });

  factory User.fromDocument(DocumentSnapshot doc){
    return User(
      uid: doc['uid'],
      email: doc['email'],
      username: doc['username']    );
  }
}


0

1 Answer 1

3

You are not initializing currentUser that's why it is equal to null. You should do the following:

  Future<DocumentSnapshot> getData() async{
    var firebaseUser = await FirebaseAuth.instance.currentUser();
    return userReference.document(firebaseUser.uid).get();
  }

So create a method that returns a Future<DocumentSnapshot> and inside that document you can retrieve the current user and use get() to retrieve the data.

Then inside the FutureBuilder, do the following:

    return FutureBuilder(
        future: getData(),

      if (!dataSnapshot.hasData) {
        return Padding(
        padding: EdgeInsets.all(17.0),
}
 else if(dataSnapshot.hasData){
      User user = User.fromDocument(dataSnapshot.data);
      return Padding(
        padding: EdgeInsets.all(17.0),
        child: Column(
          children: <Widget>[
            CircleAvatar(
                radius: 45.0,
                backgroundColor: Colors.grey,
                backgroundImage: NetworkImage(
                    'https://upload.wikimedia.org/wikipedia/commons/9/9a/Mahesh_Babu_in_Spyder_%28cropped%29.jpg')),
            Text(user.username),
            Expanded(
                flex: 1, child: Column(children: <Widget>[Text("coolbro")]))
          ],
        ),
      );
    });
 }
Sign up to request clarification or add additional context in comments.

12 Comments

Hi thanks for the help. But this time I am getting the error . The method [] was called on null.
Try if(dataSnapshot.hasData){ User user = User.fromDocument(dataSnapshot.data); inside the FutureBuilder
I have edited my profilepicture(). Should I had the if statement you suggested under my (if !dataSnapshot.hasData)
The following NoSuchMethodError was thrown building FutureBuilder<DocumentSnapshot>(dirty, state: _FutureBuilderState<DocumentSnapshot>#89c6e): The method '[]' was called on null. Receiver: null Tried calling: []("uid") The relevant error-causing widget was :When the exception was thrown, this was the stack #0 Object.noSuchMethod #1 DocumentSnapshot.[] #2 new User.fromDocument #3 _ProfilePageState.profilepicture.<anonymous closure> #4 _FutureBuilderState.build ...
I haved added the full error. Seems like the error is coming from the new method
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.