我无法在Firestore和Flutter中获得每个用户的当前文档ID

时间:2020-06-03 15:16:14

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

我在使用Firebase时遇到问题。当我尝试访问当前文档ID时,出现错误:NoSuchMenthodError:在null上调用了gtter'id'。但是,当用户注册时,我可以获取他的ID,然后在重新启动应用程序时,再次出现错误。我很确定我当前没有初始化它。 我收到错误的ProfilePage:

class ProfilePage extends StatefulWidget {
  String userprofileId;
  ProfilePage({this.userprofileId});

  @override
  _ProfilePageState createState() => _ProfilePageState();
}

class _ProfilePageState extends State<ProfilePage> {

  profilepicture() {

    return FutureBuilder(
        future:  userReference.document(widget.userprofileId).get(), //getting the error here
        builder: (BuildContext context, dataSnapshot  ) {
          if (!dataSnapshot.hasData) {
            //User user = User.fromDocument(dataSnapshot.data);
            return Text("Loading");
          }
          User user = User.fromDocument(dataSnapshot.data);
          return Text("User is ${user.username}");
        });
  }


我的用户管理,我将ID传递给他

DocumentReference ref = Firestore.instance.collection('users').document();
class Usermanagment {

  storeUser(user,email,context) async{
    ref.setData({
      'id':ref.documentID,
      'email':email+'@uselfie.com',
      'username':email,
      'uid':user.uid,
      'profilepic': 'http://www.accountingweb.co.uk/sites/all/modules/custom/sm_pp_user_profile/img/default-user.png'
    });
      DocumentSnapshot documentsnapshot = await userReference.document(ref.documentID).get();
    currentuser = User.fromDocument(documentsnapshot);

  }

}

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

  User( {
    this.uid,
    this.email,
    this.username,
    this.id,
    this.profilepic
  });
  inputData() async {
    final FirebaseUser user = await FirebaseAuth.instance.currentUser();
    final uid = user.uid;
    // here you write the codes to input the data into firestore
  }

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

我的主页,我可以控制用户的登录和注册,并带他去注册

class _HomePageState extends State<HomePage> {

  return Scaffold(
      body: PageView(
        children: <Widget>[
          TimeLinePage(),
          SearchPage(),
          ContestPage(),
          NotificationPage(),
          ProfilePage(userprofileId:currentuser.id)
        ],

0 个答案:

没有答案