0

I want to assign a variable only if the value is not null. However, I also need an extra string added to the variable to be used in video overlays (so need the extra min in this case). So I need minVal to return null if $time is null or return $time min if $time is populated. Currently, if $time is null, it still recognizes the "min" and is returning "min".

final minVal = '$time min' ?? null;

The code below works in returning null if $time is null, but I need the extra "min" string to be added somewhere. Any ideas?

final minVal = '$time' ?? null;

2 Answers 2

4

You could check if $time is null or not using ternary operator

final minVal = time != null ? '$time min' : null;

enter image description here

Sign up to request clarification or add additional context in comments.

3 Comments

Thanks for the suggestion. That seems to still be returning min even if $time is null.
SOrry forgot to remove $ in the comaprision. Updated the answer
Thank for your help! I realized I was also using an older version of Flutter still in this project (no null safety). This is the correct way to do it.
0
String ? myVariable;
Text("${myVariable!=null?myVariable:""}");

if myVariable values is not equals null then return value else make null safety

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.