Function 为什么DropdownButton必须位于名为getDropdownBotton的函数之前?
我当时正在用flifter编写一些代码,我不明白为什么要这样编写。 如果我想返回DropdownButton,为什么名称必须在函数Function 为什么DropdownButton必须位于名为getDropdownBotton的函数之前?,function,flutter,return,Function,Flutter,Return,我当时正在用flifter编写一些代码,我不明白为什么要这样编写。 如果我想返回DropdownButton,为什么名称必须在函数getDropdownBotton之前?我的意思是,为什么我必须写它,为什么没有它这个代码就不能工作?还有,为什么我必须在返回DropdownButton时写入 DropdownButton<String> getDropdownBotton() { return DropdownButton<String>( value: sel
getDropdownBotton
之前?我的意思是,为什么我必须写它,为什么没有它这个代码就不能工作?还有,为什么我必须在返回DropdownButton时写入
DropdownButton<String> getDropdownBotton() {
return DropdownButton<String>(
value: selectedCurrency,
items: getDropdownItems(),
onChanged: (value) {
setState(() {
selectedCurrency = value;
});
},
);
}
DropdownButton getdropdownboton(){
返回下拉按钮(
值:选择的货币,
items:getDropdownItems(),
一旦更改:(值){
设置状态(){
所选货币=价值;
});
},
);
}
为DropdownButton之类的东西编写
的原因是Dart(颤振使用的语言)不喜欢推断类型。当然可以,但建议您自己申报
至于为什么必须声明返回类型(方法名称之前的内容),Dart同样不喜欢推断类型。您可以删除类型下拉按钮
,Dart只会推断返回类型
getDropdownBotton() { // Dart will now infer the type and secretly, the keyword `dynamic` is there to let it know it could return anything
return DropdownButton<String>(
value: selectedCurrency,
items: getDropdownItems(),
onChanged: (value) {
setState(() {
selectedCurrency = value;
});
},
);
}
getdropdownboton(){//Dart现在将推断类型,并秘密地使用关键字'dynamic'让它知道它可以返回任何东西
返回下拉按钮(
值:选择的货币,
items:getDropdownItems(),
一旦更改:(值){
设置状态(){
所选货币=价值;
});
},
);
}
return
是必需的,因为您希望函数为您提供一些内容,在本例中,是一个下拉按钮
。如果没有关键字return
,Dart将只执行代码并将其保存到自己的位置(我知道,对吧?)。使用return关键字,现在调用此方法的小部件将能够从getDropdownBotton
中获得一个要使用的下拉按钮
p.S:您的函数拼错了,它应该是getDropdownButton
您必须返回,因为函数希望您返回类型为DropdownButton
的内容。。。但是为什么需要getdropdownboton
?您多次调用它吗?它不是必需的方法名吗?如果DropdownButton是该类型的名称。非常感谢!但我仍然不明白为什么我需要和“return”放在同一行。你能解释一下吗?我也不必包括在内,对吗?代码仍然以同样的方式工作