简体   繁体   English

Dart 多个函数调用/设置变量都在一行上

[英]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:我需要在函数开始时清除一些列表,否则它们会通过add方法变得越来越大,所以我可以使用= [].clear()来实现这一点,但格式将它们全部放在单独的行上像这样:

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?如何设置已在同一行中声明的多个变量?

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 dartpad检查

Note: There should be no trailing comma like [x,y,z , /*this one*/] or dartfmt will take it to next line. 注意:不应使用[x,y,z , /*this one*/] thisone [x,y,z , /*this one*/]或dartfmt之类的结尾逗号。

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

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. 设置null或另一个不可变值时,它会更好地工作。 It's not particularly readable either. 它也不是特别可读

There is no simple way in Dart to execute multiple expressions in a single statement without some helper. 如果没有帮助器,Dart中没有简单的方法可以在单个语句中执行多个表达式。

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. 只需编写惯用的代码,您的读者就可以更轻松地理解它,并且编译器将因此更加高效。

It's not possible to enforce a formatting rule of you own. 不可能强制执行自己的格式设置规则。

You should stick to the global formatting rules defined by dartfmt to ensure your code follows the same formatting as any other dart code. 您应遵守dartfmt定义的全局格式设置规则,以确保您的代码遵循与其他任何dart代码相同的格式。

Coming from code formatting : 来自代码格式

While your code might follow any preferred style—in our experience—teams of developers might find it more productive to: 尽管您的代码可能遵循任何首选的样式(根据我们的经验),但开发人员团队可能会发现它在以下方面更具生产力:

  • Have a single, shared style, and 具有单一的共享风格,并且
  • Enforce this style through automatic formatting. 通过自动格式化来强制这种样式。

The alternative is often tiring formatting debates during code reviews, where time might be better spent on code behavior rather than code style. 替代方法通常是在代码审查过程中引起格式辩论的麻烦,在这种情况下,最好将时间花在代码行为上而不是代码风格上。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM