flutter - 是否可以根据布尔值添加或删除列表?
问题描述
我有以下bool
bool showJustify = false;
我有以下列表:
final _valueToText = <Attribute, String>{
Attribute.leftAlignment: Attribute.leftAlignment.value!,
Attribute.centerAlignment: Attribute.centerAlignment.value!,
Attribute.rightAlignment: Attribute.rightAlignment.value!,
Attribute.justifyAlignment: Attribute.justifyAlignment.value!,
};
我想做的只是仅在以下情况下才添加Attribute.justifyAlignment: Attribute.justifyAlignment.value!
决赛showJustify = true
有没有一种简单的方法可以做到这一点?
我想只将前 3 个添加到原始列表中,然后执行
if (showJustify == true)
_valueToText.add(Attribute.justifyAlignment: Attribute.justifyAlignment.value!)
但也许有更简单的方法?
解决方案
是的,您可以使用collection-if来做到这一点。例如:
bool condition = true/false;
final map = <int, String>{
0: 'Zero',
if (condition) 1: 'One',
};
要回答您的问题:
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!,
};
推荐阅读
- javascript - 如何将 tinyMCE 内容呈现为
- ios - 如何查询整个 Realm 数据库中的特定数据,而不仅仅是特定字段或属性
- amazon-web-services - CloudFormation 模板中不接受安全组
- java - 如何将sql查询条件动态传递给@query作为参数
- android - NumberPickerPreference for androidx-preference kotlin
- android - 如何在 react-native 中从应用程序启动覆盖窗口设置
- php - 如何修复警告:为 foreach() 提供的参数无效
- ruby-on-rails - 同名控制器,一个用于管理员,一个用于用户。管理员不断路由到用户。怎么修?
- ios - iOS Swift Firestore 以及如何避免单例
- linux - 将文本导入 bash 脚本