简体   繁体   中英

flutter :NoSuchMethodError: The getter 'id' was called on null. Receiver: null Tried calling: id

When I register a new user or even log in, the user's data is not stored in currentUser, so when I want to use it to save the post data in a new collection with name posts, I get this error.NoSuchMethodError: The getter 'id' was called on null. Receiver: null Tried calling: id. alsol i get this Exception: throw new NoSuchMethodError.withInvocation(this, invocation);

my register code:

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

and her for post




  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();
    });
  }

and I use this model to save currentUser datat

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

Please try to refer to your current user like this:

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

And please provide more information. Are the two functions in the same class or widget? If yes, is it stateful and have you called

setState((){});

Maybe you must wait for the registration to complete, so you are trying to access the currentUser even though he has not been set yet?

Your error suggests that your currentUser variable is null, so you cannot use it. I suggest checking whether the currentUser is null before you perform any code that involves POSTING

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM