英文:
Is there a way to put a ternary operator between the functions of a list?
问题
我想知道在Dart中是否有可能在函数列表之间设置条件,例如如果条件为true,则使用.where
,但如果条件为false,则不进行任何操作。类似于这样:
final List<dynamic> lists = List<dynamic>.from(
lists2
.map((x) => Model.fromMap(x)) (isNotWhere) ? .first :
.where((e) => (e.deleted == deleted)),
);
因为如果不行,我唯一能想到的方法是首先获取列表,然后在代码的另一部分进行条件判断,就像这样:
List<dynamic> lists =
List<dynamic>.from(
lists2.map((x) => Model.fromMap(x))
);
if (!isNotWhere) {
lists = lists
.where((e) => !(e.deleted))
.toList();
}
请注意,以上代码片段中的条件判断可能有语法错误。如果需要更多帮助,请提供更多上下文或更多详细信息。
英文:
I would like to know if for dart there is the possibility to make a condition between functions of a list, for example if it is true to put .where but if it is false not to put anything. Something like this:
final List<dynamic> lists = List<dynamic>.from(
lists2
.map((x) => Model.fromMap(x)) (isNotWhere) ? .first :
.where((e) => (e.deleted == deleted)),
);
Because if not, the only thing I can think of is to first get the list and in another part of the code make the condition like this :
List<dynamic> lists =
List<dynamic>.from(
lists2.map((x) => Model.fromMap(x))
);
if (!isNotWhere) {
lists = lists
.where((e) => !(e.deleted))
.toList();
}
答案1
得分: 2
以下是翻译好的部分:
"For your example I would do the where
always and use the condition inside. Something like
最好的做法是针对你的示例总是使用 where
,并在内部使用条件。类似于
final List<dynamic> lists = List<dynamic>.from(
lists2
.map((x) => Model.fromMap(x))
.where((e) => (isNotWhere || (e.deleted == deleted))),
);
So basically make the where
return everything in the case that you don't want to use the where
因此,基本上让 where
在你不想使用它的情况下返回所有内容。"
英文:
For your example I would do the where
always and use the condition inside. Something like
final List<dynamic> lists = List<dynamic>.from(
lists2
.map((x) => Model.fromMap(x))
.where((e) => (isNotWhere || (e.deleted == deleted))),
);
So basically make the where
return everything in the case that you don't want to use the where
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论