简体   繁体   English

NoSuchMethodError:在 flutter 中,“在 null 上调用了 getter 'uid'

[英]NoSuchMethodError: 'The getter 'uid' was called on null' in flutter

I am pretty new to flutter.我对 flutter 很陌生。 And I am getting this NoSuchMethodError: The getter 'uid' was called on null.我得到了这个 NoSuchMethodError:在 null 上调用了 getter 'uid'。 I have red many posts and it seems like I need to use await with async.我有很多帖子,似乎我需要将 await 与 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.当我想传递用户的当前 uid 时,就会发生这种情况。 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我的用户 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']    );
  }
}


You are not initializing currentUser that's why it is equal to null.您没有初始化currentUser ,这就是它等于 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.因此,创建一个返回Future<DocumentSnapshot>的方法,在该文档中,您可以检索当前用户并使用get()检索数据。

Then inside the FutureBuilder , do the following:然后在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")]))
          ],
        ),
      );
    });
 }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 未处理的异常:NoSuchMethodError:在 null 上调用了 getter &#39;uid&#39; - Unhandled Exception: NoSuchMethodError: The getter 'uid' was called on null 在 flutter 中的 null 上调用了 getter 'uid' - The getter 'uid' was called on null in flutter 在 null.Flutter 上调用了 getter 'uid' - The getter 'uid' was called on null.Flutter 在扑火中被调用为 null 的 Getter uid - Getter uid being called on null in flutter fire Flutter:NoSuchMethodError:getter &#39;length&#39; 在 null 上被调用 - Flutter: NoSuchMethodError: The getter 'length' was called on null NoSuchMethodError:在 null 上调用了吸气剂“docs”。Flutter - NoSuchMethodError: The getter 'docs' was called on null. Flutter getter &#39;uid&#39; 被调用为 null - The getter 'uid' was called on null 例外:NoSuchMethodError:在 null 上调用了 getter 'uid'。 接收方:null 尝试调用:uid - Exception: NoSuchMethodError: The getter 'uid' was called on null. Receiver: null Tried calling: uid “NoSuchMethodError:在 null 上调用了 getter 'docs'”错误 Flutter Firebase - “NoSuchMethodError: The getter 'docs' was called on null” Error Flutter Firebase Firebase/Flutter:未处理的异常:NoSuchMethodError:在 null 上调用了 getter 'isNotEmpty' - Firebase/Flutter : Unhandled Exception: NoSuchMethodError: The getter 'isNotEmpty' was called on null
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM