ashf
ashf

Reputation: 217

'If null' operator to return an empty String

I am doing a null check with the operator '??'.

Text(contact.rating.toString() ?? " ",

However the text shows null when it is null, instead of

What's the best way to write this?

Upvotes: 0

Views: 807

Answers (2)

ikerfah
ikerfah

Reputation: 2862

Because contact.rating is null, so you have to do the following

Text(contact.rating?.toString() ?? " "),

Upvotes: 3

Will Hlas
Will Hlas

Reputation: 1341

Here's one way:

Text(contact.rating != null ? contact.rating.toString() : " ")

Upvotes: 0

Related Questions