In Flutter/Dart the examples sometimes show fat arrow and sometimes dont. Here are examples:
RaisedButton(
onPressed: () {
setState(() {
_myTxt = "Text Changed";
});
},
Elsewhere you see:
void main() => runApp(MyApp());
In Flutter/Dart the examples sometimes show fat arrow and sometimes dont. Here are examples:
RaisedButton(
onPressed: () {
setState(() {
_myTxt = "Text Changed";
});
},
Elsewhere you see:
void main() => runApp(MyApp());
The fat arrow syntax is simply a short hand for returning an expression and is similar to
(){ return expression; }
.According to the docs.
From the code above, You can see that multiline statement can be made when the callback function is used and then a value is returned, while the fat arrow simply has an expression with no return keyword.
Considering your answer about fat arrows not supporting multiline statements in dart. This is quite understandable since doing
() => {somtheing}
would imply you are returning a map and it would expect to see something like() => {'name':'John', 'age':25}
and not() => { _myTxt = "Text Changed";_myTxt = "Never Mind"; }
.They are both for expressing anonymous functions. The fat arrow is for returning a single line, braces are for returning a code block.
A fat arrow trying to return a code block will not compile.
I found that the mean the exact same thing. The only difference is that you can use (you don't have to) the fat arrow if there is only one statement. Following is the above
RaisedButton
declaration with the fat arrow. Notice I had to remove two curly braces and one semi-colon:If you are used to other languages that allow you to put multiple statements after a fat arrow you'll you'll find that you can't in dart and if you try you'll get an error as in the following:
this wont work
=>
is used to return a value of an anonymous function.() {}
lets you execute multiple statements.while
() => {myVar}
or() => myVar;
allows one single statement.() => myVar;
is short and simple when returning one statement.The same logic goes for creating non anonymous functions too.
Single statement func
func() => y = x + x;
Multiple statement func