Hasen
Hasen

Reputation: 12314

Dart multiple function calls/set variables all on one line

I need to clear some Lists at the start of a function otherwise they just get larger and larger with an add method, so I can either use = [] or .clear() to achieve this, but the formatting puts them all on separate lines like this:

listone.clear();
listtwo.clear();
three.clear();
listfour.clear();
listfive.clear();
listsix.clear();

I want to do something like this but it won't allow it:

listone.clear(), listtwo.clear(), three.clear(), listfour.clear(), listfive.clear(), listsix.clear();

or

listone = [], listtwo = [], three = [], listfour = [], listfive = [], listsix = [];

How do I set multiple variables that have already been declared all on the same single line?

Upvotes: 1

Views: 2445

Answers (2)

lrn
lrn

Reputation: 71653

The easiest way to make complex things fit in less space is to use a helper method.

clearAll(Iterable<List<Object>> lists) {
  for (var list in lists) list.clear();
}

then you just write:

clearAll([listone, listtwo, three, listfour, listfive, listsix]);

You can inline the helper function

for (var l in [listone, listtwo, three, listfour, listfive, listsix]) l.clear();

but it probably won't fit on one line anyway.

If you are setting variables, and you set the variables to the same value, then you can do multiple assignments as a single expression:

listone = listtwo = three = listfour = listfive = listsix = [];

Not really recommended for lists because then all variables will point to the same list object. It works better when assinging null or another immutable value. It's not particularly readable either.

There is no simple way in Dart to execute multiple expressions in a single statement without some helper.

You could declare:

void do6(void v1, void v2, void v3, void v4, void v5, void v6) {}
...
do6(listone.clear(), listtwo.clear(), three.clear(), 
    listfour.clear(), listfive.clear(), listsix.clear());

but you will likely find that will be formatted on more than one line as well

Alternatively, just use a list literal instead of a function parameter list to allow multiple expressions:

[listone.clear(), listtwo.clear(), three.clear(), 
 listfour.clear(), listfive.clear(), listsix.clear()];

Same caveats about length and formatting, and it's confusion and inefficient that the list is created and then not used.

All in all, please don't try to be clever with formatting. Just write idiomatic code, your readers will have a much easier time understanding it, and the compiler will be more efficient that way.

Upvotes: 4

Doc
Doc

Reputation: 11651

This is what you can do

void main() {
  var listone = [1, 2], listtwo = [1, 1, 1], listthree = [0, 0, 1];

  [listone, listtwo, listthree].forEach((l) => l.clear());

  print(listone);
  print(listtwo);
  print(listthree);
}

Check at dartpad

Note: There should be no trailing comma like [x,y,z , /*this one*/] or dartfmt will take it to next line.

EDIT:

Also, if using the same thing in multiple files then

put in a global file

clearList(List list)=>list.clear();

and call something like

[listone, listtwo, listthree].forEach(clearList);

Upvotes: 2

Related Questions