简体   繁体   English

各种widget的()的flutter含义

[英]flutter meaning of () for various widgets

Ofter times in flutter I come across () used in the code, and I've just been rote memorizing when they need to be there.在颤振中,我经常会遇到代码中使用的 (),并且我只是死记硬背它们何时需要出现在那里。 However, I don't exactly understand what they mean, or even what they are called.但是,我不完全明白它们的意思,甚至不明白它们叫什么。

Some examples:一些例子:

setState((){count++})

FlatButton(child: Text("More Beer"), 
           onPressed: ()=> sendBeer())   // <-- I'm also interested if the two ()'s mean different things.

for sendBeer(), I would call the () and indication that sendBeer is a function, and that it takes no arguments.对于sendBeer(),我会调用() 并指示sendBeer 是一个函数,并且它不接受任何参数。 Is that different from () by itself?这与 () 本身不同吗?

In short, can you explain what "()" in those examples are, what they are called, and what they mean?简而言之,你能解释一下这些例子中的“()”是什么,它们叫什么,它们是什么意思? [Also, sometimes I see it as (_) or even (_,_,_) ] [此外,有时我将其视为(_)甚至(_,_,_) ]

This is unrelated to widgets/flutter.这与小部件/颤动无关。 It is the syntax for defining closures, which are functions as variables.它是定义闭包的语法,闭包是作为变量的函数。

There are two syntaxes available:有两种可用的语法:

() => value;

and

() {
  return value;
}

Where () represents the arguments of your function.其中()表示函数的参数。 It doesn't have to be strictly () and could be something more complex:它不必是严格的()并且可以是更复杂的东西:

final example = (int variable, {String namedParameter}) => print($variable $namedParameter');

Which is equivalent to:这相当于:

void example(int variable, {String namedParameter}) {
  print($variable $namedParameter');
}

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

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