The property 'id' can't be unconditionally accesed because the receiver can be 'null'

451

When you are writing currentUser!.id then you are ensuring that the currentUser will not be null. so when you the currentUser is null then you will get this error : Null check operator used on a null value. so you have to make profile id nullable if there is chance that you might get null value upon calling profileId. to do so, just declare id as nullable in User class by String? id. or another approach I recommend is to use ?? operator (reference) which is just a condition which represents if null. you can use this in the assignment statement of your UserId. like this :

body: PageView(
    children: <Widget>[
      //Feed(),
      ElevatedButton(
        child: Text('Cerrar sesión'),
        onPressed: logout,
      ),
      SubirPost(currentUser: currentUser),
      EditarPerfil(profileId: currentUser.id ?? ''),   // this is a check if the id is not null then it will return the actual value otherwise it will return the empty string.
    ],
    controller: pageController,
    onPageChanged: onPageChanged,
    physics: NeverScrollableScrollPhysics(),
  ),
Share:
451
Gian Ontiveros
Author by

Gian Ontiveros

Updated on December 31, 2022

Comments

  • Gian Ontiveros
    Gian Ontiveros over 1 year

    I'm trying to use (profileId: currentUser.id) in:

    body: PageView(
        children: <Widget>[
          //Feed(),
          ElevatedButton(
            child: Text('Cerrar sesión'),
            onPressed: logout,
          ),
          SubirPost(currentUser: currentUser),
          EditarPerfil(profileId: currentUser.id),
        ],
        controller: pageController,
        onPageChanged: onPageChanged,
        physics: NeverScrollableScrollPhysics(),
      ),
    

    but it gives me this error:

    The property 'id' can't be unconditionally accessed because the receiver can be 'null'.
    Try making the access conditional (using '?.') or adding a null check to the target ('!').
    

    I've already tried with the null check ( like this: profileId: currentUser!.id )but when I debug it throws a Null check operator used on a null value error

    id is declared like this on a user model file:

    class User {
      final String id;
      final String username;
      final String email;
      final String photoUrl;
      final String displayName;
      final String bio;
    
    User(
      {required this.id,
      required this.username,
      required this.email,
      required this.photoUrl,
      required this.displayName,
      required this.bio});
    
    factory User.fromDocument(DocumentSnapshot doc) {
    return User(
      id: doc['id'],
      email: doc['email'],
      username: doc['username'],
      photoUrl: doc['photoUrl'],
      displayName: doc['displayName'],
      bio: doc['bio'],
    );}}
    

    for profileId is this on another file:

     final String profileId;
     EditarPerfil({required this.profileId});
    

    I've tried everything I've found

    Also, when I try to use ? on the user model final String id I get the error The argument type 'String?' can't be assigned to the parameter type 'String'

    Same with the final String profileId

    I'm importing the cloud_firestore, firebase_storage and google_sign_in packages.

    • Alex Meuer
      Alex Meuer over 2 years
      Your issue is that the currentUser variable is null, not that the id is null. When your app first loads, it hasn't got the snapshot from firestore yet, so your user is null. I think your two best options are to either handle the possibility that the user is null and show a loading indicator (or similar), or initialise the currentUser as a User with empty values (i.e. id = ""). The best solution for you will depend on the architecture of your app.
  • Gian Ontiveros
    Gian Ontiveros almost 3 years
    When I include the currentUser.id ?? ' ' it gives me the error "the left operand can't be null, so the right operand is never executed (dead null aware)" so I declare 'String id' as 'String? id' in the User class and it resolves the dead null aware but keeps the error mentioned in the main question.
  • Aayush Shah
    Aayush Shah almost 3 years
    If you are getting null value when accessing userId then this ?? operator will replace that with an empty string ''. Now Please try to update the question and error message after this.