flutter - 如何在不同类的迭代器 FOR 中传递变量列表?
问题描述
我试图将变量 List _cities 放在不同的类中,然后使用迭代器“for”传递它们,但在“迭代器”中显示错误。请帮助我如何在不同类的迭代器中传递变量 List _cities?[ https://i.stack.imgur.com/kVTbj.jpg ]
class romania_state {
romania_state({this.state});
List<String> state = [
"",
"Cluj-Napoca",
"Bucuresti",
];
}
class romania_city {
romania_city({this.Cluj-Napoca, this.Bucuresti});
List<String> Cluj-Napoca = [
"",
"A",
"B"
];
List<String> Bucuresti = [
"",
"Y",
"Z"
];
}
class RegisterPageBodyState extends State<RegisterPageBody>
with SingleTickerProviderStateMixin {
romania_state romaniaState = new romania_state();
romania_city romaniaCity = new romania_city();
List<DropdownMenuItem<String>> _dropDownMenuStates;
List<DropdownMenuItem<String>> getDropDownMenuState() {
List<DropdownMenuItem<String>> state1 = List();
for (String statelist in romaniaState.state) { //Error in here
state1.add(DropdownMenuItem(value: statelist, child: Text(statelist)));
}
return state1;
}
List<DropdownMenuItem<String>> getDropDownMenuCluj-Napoca() {
List<DropdownMenuItem<String>> Cluj-Napoca1 = List();
for (String Cluj-Napocalist in romaniaCity.Cluj-Napoca) { //Error in here
Cluj-Napoca1.add(DropdownMenuItem(value: Cluj-Napocalist, child:
Text(Cluj-Napocalist)));
}
return Cluj-Napoca1;
}
List<DropdownMenuItem<String>> getDropDownMenuBucuresti() {
List<DropdownMenuItem<String>> Bucuresti1 = List();
for (String Bucurestilist in romaniaCity.Bucuresti) { //Error in here
Bucuresti1.add(DropdownMenuItem(value: Bucurestilist, child:
Text(Bucurestilist)));
}
return Bucuresti1;
}
}
解决方案
尝试从列表名称中删除下划线 (_)。在变量的开头添加下划线使其在 Dart 中“私有”,如此处所述。
推荐阅读
- oracle - Oracle NUM_CPUS、NUM_CPU_SOCKETS 和连接池
- javascript - 如何在 React 中通过 POST API 调用渲染数据
- haxe - 如何更改堆中的字体大小?
- r - 如何在 r 中执行 kruskal wallis 和 dunns 测试
- r - R中最简单的主题分配算法
- ssas - 无法通过 ODBC 使用 Snowflake 后端处理 SSAS 表格
- flutter - Flutter 插件中的 setDataSource 错误 VideoThumbnail
- r - 反转主要 y 轴并反转次轴
- c# - 在路径中使用 DateTime 和 Double 变量进行 Web api 搜索
- typescript - 获取分隔符中的字符