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

Viewed 623

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.

2 Answers

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(),
  ),

Use profileId: currentUser!.id

Here you are ensuring that currentUser will definitely have a property of id

Related