Fayakon
Fayakon

Reputation: 1523

Flutter invalid refrence to 'this' expression

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

List Section Method:

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

Upvotes: 5

Views: 7451

Answers (1)

Juanes30
Juanes30

Reputation: 2556

change this:

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

for this:

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

Upvotes: 3

Related Questions