Aseem
Aseem

Reputation: 6779

ListView doesnot show updated list value. But column does

I have a list

List tasksList = <Widget>[
    Text('task1'),
    Text('task2'),
  ];

In the code when user adds new task I add it to the list as follows:

widget.tasksList.add(
   Text('task3'),
);

An expanded widget column shows the recent addition of 'task3' nicely:

Column(
  children: this.tasksList,
),

But when I use ListView I dont see 'task3'

ListView(
  children: this.tasksList,
),

Is there something special I need to do to refresh the ListView as compared to a column widget? Thanks

Upvotes: 0

Views: 29

Answers (1)

Evan
Evan

Reputation: 880

Two solutions I know of

1) Use ListView.builder

ListView.builder(
    itemCount: tasksList.length,
    itemBuilder: (BuildContext context, int index) {
        return tasksList[index];
    }
)

2) Wrap column inside listview

ListView(
    children: <Widget>[
        Column(
            children: tasksList,
        )
    ],
)

Upvotes: 1

Related Questions