NoSuchMethodError:在颤振中'将吸气剂'uid'调用为null'

时间:2020-06-02 17:37:32

标签: firebase flutter dart google-cloud-firestore firebase-authentication

我是个新手。而且我收到此NoSuchMethodError:在null上调用了getter'uid'。我有很多帖子,似乎需要使用await和async。但是我不知道我该怎么做。当我要传递用户的当前uid时,就会发生这种情况。 这是我的代码:

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")]))
              ],
            ),
          );
        });
  }

我的用户模型

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']    );
  }
}


1 个答案:

答案 0 :(得分:2)

您没有初始化currentUser,因此它等于null。您应该执行以下操作:

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

因此,请创建一个返回Future<DocumentSnapshot>的方法,然后在该文档中可以检索当前用户并使用get()来检索数据。

然后在FutureBuilder内执行以下操作:

    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")]))
          ],
        ),
      );
    });
 }