user16329512
user16329512

Reputation:

Error: The argument type 'String?' can't be assigned to the parameter type 'String' because 'String?' is nulable and 'String' isn't 'Email'+user.email

Can Someone Help Me to remove this error

Upvotes: 0

Views: 336

Answers (2)

BLKKKBVSIK
BLKKKBVSIK

Reputation: 3556

The error you're facing came from null-safety, the argument that you're sending to NetworkImage widget can be either null or either a String, so you may want to update your code to this example:

NetworkImage(user.photoURL!),


Text("Name ${user.displayedName!}", style: TextStyle(color: Colors.white)),

You can learn more about null safety on official documentation:

https://flutter.dev/docs/null-safety

Upvotes: 0

enzo
enzo

Reputation: 11496

You must handle the cases when 1) the user doesn't have a profile picture and 2) the user doesn't have a display name. You can handle this by using the ternary operator:

@override
Widget build(BuildContext context) {
  final user = FirebaseAuth.instance.currentUser;
  final String? url = user.photoURL;
  final String? name = user.displayName;

  return Container(
    // ...

    // Handling the profile picture
    url == null 
      ? SizedBox.shrink()    // If it's missing, display an empty box
      : CircleAvatar(radius: 25, backgroundImage: NetworkImage(url))
    
    // ...

    // Handling the display name
    name == null
      ? Text("")    // If it's missing, display an empty text
      ? Text("Name: ${name}", style: TextStyle(color: Colors.white))
  );
}

Upvotes: 1

Related Questions