Daniel Roldán
Daniel Roldán

Reputation: 1556

Is there a way to put a ternary operator between the functions of a list?

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();
        }

Upvotes: 1

Views: 51

Answers (1)

Ivo
Ivo

Reputation: 23257

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

Upvotes: 2

Related Questions