Search code examples
flutterdartnulldart-null-safety

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


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;

Solution

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

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

    enter image description here