本文介绍了颤振对“this"表达式的无效引用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我单击添加按钮时,我正在尝试在列表中添加项目,所有代码都正常,但我收到错误无效引用 this 表达式.我正在使用 stateful 小部件.

i am trying to add item in list when i click on add button, all code is ok, but i am getting error invalid reference to this expression. i am using stateful widget.

List<Widget> _listSection = [];
            body: Container(
              child: Stack(
                children: [

                  FloatingActionButton(
                    onPressed: () {
                      _listSection.add(
                        listSectionMethod(
                            "title three", "hello from click", Icons.forward),
                      );

                      setState(() {});
                    },
                  ),
                ],
              ),
            ),
          ),
        );
      }
    }

    Widget listSection = Container(
      margin: EdgeInsets.only(top: 210),
      child: ListView(
        children: [
          Column(
            children: [
              Column(
                children: this._listSection, // ----> ERROR HERE
              ),
            ],
          ),
        ],
      ),
    );

列表部分方法:

Card listSectionMethod(String title, String subtitle, IconData icon) {
  return new Card(
    child: ListTile(
      title: Text(
        title,
        style: TextStyle(fontWeight: FontWeight.bold),
      ),
      subtitle: Text(subtitle),
      trailing: Icon(
        icon,
        color: Colors.blue,
      ),
    ),
  );
}

推荐答案

改变这个:

Widget listSection = Container(
      margin: EdgeInsets.only(top: 210),
      child: ListView(
        children: [
          Column(
            children: [
              Column(
                children: this._listSection,
              ),
            ],
          ),
        ],
      ),
    );

为此:

Widget listSection() {
        return Container(
          margin: EdgeInsets.only(top: 210),
          child: ListView(
            children: [
              Column(
                children: this._listSection,
              ),
            ],
          ),
        );
     }

这篇关于颤振对“this"表达式的无效引用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-25 12:40