首页 > 解决方案 > 如何在不同类的迭代器 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;
  }
}

标签: flutter

解决方案


尝试从列表名称中删除下划线 (_)。在变量的开头添加下划线使其在 Dart 中“私有”,如此处所述


推荐阅读