Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Is it possible to add or remove to a list based on a bool value?

I have the following bool

bool showJustify = false;

and I have the following list:

    final _valueToText = <Attribute, String>{
      Attribute.leftAlignment: Attribute.leftAlignment.value!,
      Attribute.centerAlignment: Attribute.centerAlignment.value!,
      Attribute.rightAlignment: Attribute.rightAlignment.value!,
      Attribute.justifyAlignment: Attribute.justifyAlignment.value!,
    };

What I’d like to do is only add the final Attribute.justifyAlignment: Attribute.justifyAlignment.value! only if showJustify = true

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

Is there a simply way to accomplish this?

I was thinking of only adding the first 3 to the original list and then doing

if (showJustify == true)
_valueToText.add(Attribute.justifyAlignment: Attribute.justifyAlignment.value!) 

But maybe there is a simpler way?

>Solution :

Yes, you can do that using conditional if. For example:

bool condition = true/false;
final map = <int, String>{
  0: 'Zero',
  if (condition) 1: 'One',
};

To answer your question:

final _valueToText = <Attribute, String>{
 Attribute.leftAlignment: Attribute.leftAlignment.value!,
 Attribute.centerAlignment: Attribute.centerAlignment.value!,
 Attribute.rightAlignment: Attribute.rightAlignment.value!,
 if (showJustifty) Attribute.justifyAlignment: Attribute.justifyAlignment.value!,
};
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading