对'this'表达式的无效引用

时间:2019-12-12 21:14:04

标签: flutter flutter-layout

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

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,
      ),
    ),
  );
}

1 个答案:

答案 0 :(得分:1)

更改此内容:

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, 
              ),
            ],
          ),
        );
     }