Pretty_Girl
Pretty_Girl

Reputation: 327

check if null produces errors - can't be unconditionally accessed because the receiver can be 'null'

I wanna check if the Icon doesn't return null and if it returns null then show the SvgPic , or else show the Icon .

Errors:

  1. Too many positional arguments,
  2. The property 'icon' 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 ('!').

.

final SvgPicture? svgPic; final Icon? icon;
child: Container(widget.icon == null
                                ? widget.svgPic
                                : Icon(widget.icon.icon,
                                    color: widget.iconColour)),
                          ),

Upvotes: 1

Views: 46

Answers (1)

Midhun MP
Midhun MP

Reputation: 107121

  • Too many positional arguments

You didn't add any parameter label in that container

  • The property 'icon' 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 ('!')

You are trying to access the icon property of a nullable, so you need to use ? or !.

Instead of:

Container(widget.icon == null ? widget.svgPic
   : Icon(widget.icon.icon, color: widget.iconColour),
),

Use:

Container(child: widget.icon == null ? widget.svgPic
   : Icon(widget.icon!.icon, color: widget.iconColour)
),

Upvotes: 1

Related Questions