将下拉按钮添加到选项卡时,只能在初始化程序中访问静态成员

only static members can be accessed in initializers when adding dropdownbutton to tab

我的应用程序包含 TapView,其中一个选项卡包含一个 DropDownButton,当创建 DropDownButton 时它拒绝它并产生错误,在初始化程序中只能访问静态成员

所以我尝试在 initState() 中初始化它并像这里一样在 tap 中调用它,但它给了我空指针异常:

class _MyHomePageState extends State<MyHomePage> {

  static DropdownButton<ExpenseType> drop ;
  ExpenseType Drop_value = new ExpenseType("tt", Icons.directions_bus);
  List<ExpenseType> DropDownValues = new List<ExpenseType>();
  void onChange_Drop(ExpenseType value){
    setState(() {
      Drop_value = value;
    });
  }


  @override
  void initState() {


    drop =  new DropdownButton(value:Drop_value,
        items: DropDownValues.map((ExpenseType value){
          return new DropdownMenuItem(
              value: value,
              child: Row(
                children: <Widget>[
                  new Text(value.expType),
                ],
              ));
        }).toList(),
        onChanged: (ExpenseType value){onChange_Drop(value);});
    DropDownValues.addAll([
       ExpenseType("transportation",Icons.directions_bus),
       ExpenseType("food",Icons.fastfood),
       ExpenseType("drink",Icons.local_drink),
       ExpenseType("shopping",Icons.shopping_basket),
       ExpenseType("others",Icons.menu),
    ]);

    Drop_value = DropDownValues.elementAt(0);
    super.initState()
}

  List<Widget> containers = [
    Container(
      child: new SingleChildScrollView(
        // the main content container
        child: new Column(
          children: <Widget>[
//Type container
                            new Column(
                              children: <Widget>[
                                new Text(
                                  "hello",
                                  textAlign: TextAlign.left,
                                  style: new TextStyle(color: Colors.black),
                                  textDirection: TextDirection.ltr,
                                ),
                                drop,

                              ],
                            ),
                          ],
                        ),
    Container(),
    Container(),
  ];
  @override
  Widget build(BuildContext context) {
    // This method is rerun every time setState is called, for instance as done
    // by the _incrementCounter method above.
    //
    // The Flutter framework has been optimized to make rerunning build methods
    // fast, so that you can just rebuild anything that needs updating rather
    // than having to individually change instances of widgets.
    return DefaultTabController(
      length: 3,
      child: Scaffold(
        appBar: new AppBar(
          // Here we take the value from the MyHomePage object that was created by
          // the App.build method, and use it to set our appbar title.

          title: Center(
            child: Row(
              mainAxisAlignment: MainAxisAlignment.center,
              children: <Widget>[
                new Icon(
                  Icons.monetization_on,
                  color: Colors.black,
                ),
                new Text(" Expensia"),
              ],
            ),
          ),
          bottom: TabBar(
            tabs: <Widget>[
              Tab(
                text: "Add",
              ),
              Tab(
                text: "Prev Expenses",
              ),
              Tab(
                text: "Statistics",
              ),
            ],
          ),
        ),
        body: TabBarView(children: containers),
      ),
    );
  }
}

您的容器 List 以及您的 DropdownButton 应该在您的 build 函数中实例化。这可确保在 Flutter 需要时重建所有小部件(例如 setState())。

这是你的一些修改版本,如果你只用你的模型替换它应该可以工作 类(我的 String

class _MyHomePageState extends State<MyHomePage> {
  String dropValue;
  List<String> dropDownValues;

  void onChangeDrop(String value) {
    setState(() {
      dropValue = value;
    });
  }

  @override
  void initState() {
    dropDownValues = [
      "transportation",
      "food",
      "drink",
      "shopping",
      "others",
    ];

    dropValue = dropDownValues[0];
    super.initState();
  }

  @override
  Widget build(BuildContext context) {
    return DefaultTabController(
      length: 3,
      child: Scaffold(
        appBar: new AppBar(
          title: Center(
            child: Row(
              mainAxisAlignment: MainAxisAlignment.center,
              children: <Widget>[
                new Icon(
                  Icons.monetization_on,
                  color: Colors.black,
                ),
                new Text(" Expensia"),
              ],
            ),
          ),
          bottom: TabBar(
            tabs: <Widget>[
              Tab(text: "Add"),
              Tab(text: "Prev Expenses"),
              Tab(text: "Statistics"),
            ],
          ),
        ),
        body: TabBarView(children: [
          Container(
              child: new SingleChildScrollView(
                  child: new Column(
            children: <Widget>[
              new Column(
                children: <Widget>[
                  new Text(
                    "hello",
                    textAlign: TextAlign.left,
                    style: new TextStyle(color: Colors.black),
                    textDirection: TextDirection.ltr,
                  ),
                  DropdownButton(
                      value: dropValue,
                      items: dropDownValues.map((String value) {
                        return new DropdownMenuItem(
                            value: value, child: new Text(value));
                      }).toList(),
                      onChanged: (String value) {
                        onChangeDrop(value);
                      }),
                ],
              ),
            ],
          ))),
          Container(),
          Container(),
        ]),
      ),
    );
  }
}