flutter :NoSuchMethodError: getter 'id' 在 null 上被调用。接收者:空尝试调用:id

时间:2021-03-04 13:10:18

标签: ios firebase flutter google-cloud-firestore

当我注册一个新用户甚至登录时,用户的数据并没有存储在currentUser中,所以当我想用它来将post数据保存在一个名为posts的新集合中时,我得到这个错误。NoSuchMethodError: getter 'id' 被调用为 null。接收者:null 尝试调用:id。 我也得到这个例外: throw new NoSuchMethodError.withInvocation(this, invocation);

我的注册码:

  Future<void> registerWithEmailAndPassword(
      String email, String password, username) async {
    print("registering now");
    try {
      final authResult = await FirebaseAuth.instance
          .createUserWithEmailAndPassword(email: email, password: password);
      var _uid = authResult.user.uid;
      // DocumentSnapshot doc = await usersRef.doc(user.id).get();
      print("this is userid: $_uid");
      createUserInFirestore(authResult, username);

   
      return _userFromFirebase(authResult.user);
    } catch (e) {
      print(e.toString());
      return null;
    }
  }

  Future<void> logout() async {
    await FirebaseAuth.instance.signOut();
  }

    Future<void>createUserInFirestore(authResult, username) async {

   
    var User user= FirebaseAuth.instance.currentUser;
    
    DocumentSnapshot doc = await usersRef.doc(user.uid).get();

    if (!doc.exists) {
collection
      usersRef.doc(user.uid).set({
        "userid": user.uid,
        "username": username,
        "photoUrl": user.photoURL,
        "email": user.email,
        "displayName": user.displayName,
        "bio": "",
        "timestamp": timestamp
      });

      doc = await usersRef.doc(user.uid).get();
    }

    currentUser = Users.fromDocument(doc);
  }

和她发帖




  createPostInFirestore(
      {String mediaUrl, String location, String description}) {
    postsRef
        .doc(widget.currentUser.id)
        .collection("userPosts")
        .doc(postId)
        .set({
      "postId": postId,
      "ownerId": widget.currentUser.id,
      "username": widget.currentUser.username,
      "mediaUrl": mediaUrl,
      "description": description,
      "location": location,
      "timestamp": timestamp,
      "likes": {},
    });
  }

  handleSubmit() async {
    setState(() {
      isUploading = true;
    });
    await compressImage();
    String mediaUrl = await uploadImage(file);
    createPostInFirestore(
      mediaUrl: mediaUrl,
      location: locationController.text,
      description: captionController.text,
    );
    setState(() {
      file = null;
      isUploading = false;
      postId = Uuid().v4();
    });
  }

我用这个模型来保存当前用户数据

import 'package:cloud_firestore/cloud_firestore.dart';

class Users {
  final String id;
  final String username;
  final String email;
  final String photoUrl;
  final String displayName;
  final String bio;

  Users({
    this.id,
    this.username,
    this.email,
    this.photoUrl,
    this.displayName,
    this.bio,
  });

  factory Users.fromDocument(DocumentSnapshot doc) {
    return Users(
      id: doc['userid'],
      email: doc['email'],
      username: doc['username'],
      //photoUrl: doc['photoUrl'],
      //displayName: doc['displayName'],
      bio: doc['bio'],
    );
  }
}

1 个答案:

答案 0 :(得分:0)

请尝试这样称呼您当前的用户:

//you were missing 'this'
this.widget.currentUser.id

请提供更多信息。这两个函数是否在同一个类或小部件中?如果是,它是有状态的吗?你打电话了吗

setState((){});

也许您必须等待注册完成,因此即使尚未设置 currentUser,您也试图访问他?

您的错误表明您的 currentUser 变量为空,因此您无法使用它。我建议在执行任何涉及 POSTING 的代码之前检查 currentUser 是否为 null