I am very new to Dart/Flutter and I have a confusion regarding the => notation. The documentation says that the => notation is used as a shorthand to return a single expression.
bool isNoble(int atomicNumber) => _nobleGases[atomicNumber] != null;
当我试图在Flutter 应用程序中设置状态时,我会产生怀疑.
RaisedButton(
onPressed: () => {
setState(() {
print('hello');
_products.add('More stuff');
})
},
child: Text('Add Product'),
),
Now when i change the setState method with => notation
RaisedButton(
onPressed: () => {
setState(() => {
print('hello'),
_products.add('More stuff'),
})
},
child: Text('Add Product'),
),
上面提到的两种方法都有效,即它们按预期设置状态.我所要做的就是在使用胖箭头符号时将分号改为逗号.
这背后的逻辑是什么?胖箭头符号如何处理包含多个表达式的花括号.
Edit个
As mentioned by Hemanth Raj the => returns a set and the code segment containing the => notation can be written as follows.
RaisedButton(
onPressed: () => {
setState(() {
return {
print('hello'),
_products.add('More stuff'),
};
})
},
child: Text('Add Product'),
),
包含打印函数和_products t.add的返回set
实际上是如何更新状态的.它不应该抛出某种错误吗?因为setState通常是由_products.add('More stuff');
这样的表达式完成的.