Home > OS >  Flutter: Assign Variable With String Only If Value Is Not Null
Flutter: Assign Variable With String Only If Value Is Not Null

Time:09-01

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;

CodePudding user response:

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

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

enter image description here

CodePudding user response:

String ? myVariable;
Text("${myVariable!=null?myVariable:""}");

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

  • Related