How to return back an element of list - Flutter

389 Views Asked by At

I have these three lists in my app, but I can return only one of them, how can I do that to the three of them?

class TaskList extends StatelessWidget {
TaskList(this._tasks, this._starTime, this._endTime, this._onClick);

final List<Task> _tasks;
final List<Task> _starTime;
final List<Task> _endTime;
final _onClick;


@override
Widget build(BuildContext context) {
return ListView(
    children: _tasks.map((task)
    {
      return TaskItem(task,_starTime,_endTime,_onClick);
    }
    ).toList());

 }
}
1

There are 1 best solutions below

4
On

ListView only accept one List, so to do this you need to combine all the list like

  List<String> list1 = ['A', 'B'];
  List<String> list2 = ['C', 'D'];
  List<String> list3 = ['E', 'F'];
  var newList = [...list1, ...list2, ...list3];
  print(newList);

Output:

[A, B, C, D, E, F]

Now use newList inside ListView to render List

Code:

@override
Widget build(BuildContext context) {
var _newList = [..._tasks, ..._starTime, ..._endTime];
return ListView(
    children: _newList.map((task)
    {
      return TaskItem(task,_starTime,_endTime,_onClick);
    }
    ).toList());

 }
}