有没有办法在列表的函数之间放置一个三元运算符?

huangapple go评论55阅读模式
英文:

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&lt;dynamic&gt; lists = List&lt;dynamic&gt;.from(
          lists2
              .map((x) =&gt; Model.fromMap(x)) (isNotWhere) ? .first :
              .where((e) =&gt; (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&lt;dynamic&gt; lists =
            List&lt;dynamic&gt;.from(
          lists2.map((x) =&gt; Model.fromMap(x))
        );

        if (!isNotWhere) {
          lists = lists
              .where((e) =&gt; !(e.deleted))
              .toList();
        }

答案1

得分: 2

以下是翻译好的部分:

"For your example I would do the where always and use the condition inside. Something like

最好的做法是针对你的示例总是使用 where,并在内部使用条件。类似于

final List&lt;dynamic&gt; lists = List&lt;dynamic&gt;.from(
      lists2
          .map((x) =&gt; Model.fromMap(x))
          .where((e) =&gt; (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&lt;dynamic&gt; lists = List&lt;dynamic&gt;.from(
      lists2
          .map((x) =&gt; Model.fromMap(x))
          .where((e) =&gt; (isNotWhere || (e.deleted == deleted))),
);

So basically make the where return everything in the case that you don't want to use the where

huangapple
  • 本文由 发表于 2023年2月10日 16:06:56
  • 转载请务必保留本文链接:https://go.coder-hub.com/75408393.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定