Reload listview.builder using a ToggleSwitch Flutter

Viewed 19

I decided to use ToggleSwitch to try and filter out items from a list and to reload the listview to showcase it. Here's the code of the data being loaded:

@Override 
Widget build(BuildContext context) {
 return FutureBuilder<ApiCallResponse>(
  future: GetCandListByManagerCall.call(
   entityId: widget.entityId,
),
builder: (context, snapshot) {
final listViewGetCandListByManagerResponse = 
snapshot.data!;
final data = listViewGetCandListByManagerResponse.jsonBody['candList'] as List;
List<CandModel> cands = data.map((e) => CandModel.fromJson(e)).toList();
List<CandModel> filteredCands = cands;
DateTime now = new DateTime.now();
DateTime CurrentDate = new DateTime(now.year, now.month, now.day);

Here's the code of the ToggleSwitch as of right now

Expanded(
 child: ToggleSwitch(
  initialLabelIndex: 1,
  totalSwitches: 2,
  activeBgColor: [Color(0xFF33CCCC)],
  labels: ['Future Events', 'Past Events']
  onToggle: (index) {
   print('switched to $index');
   if (index == 1)
    checkWaitingEvents();
),),

The listview itself:

return ListView.builder(
 padding: EdgeInsets.zero,
 scrollDirection: Axis.vertical,
 itemCount: data.length,
 itemBuilder: (context, dataIndex) {
  List<CandModel> checkWaitingEvents()
  {
   DateTime expirationDate = DateTime.parse(cands[dataIndex].dateEvent);
   if (expirationDate.isBefore(currentDate))
     filteredCands.add(cands[dataIndex]);
   return filteredCands;
  }
  String date = (filteredCands[dataIndex].dateEvent).toString();
  DateTime tempDate = DateTime.parse(date);
  String exDate = DateFormat.yMMMd().format(tempDate);
  return InkWell(
   child: SworkerContainerWidget(
    desc: filteredCands[dataIndex].descEvent,
    fname: filteredCands[dataIndex].wfirstName,
    lname: filteredCands[dataIndex].wlastName,
    dateEvent: exDate,
  ),);},);

As of right now, the listView is showing up the list unfiltered. The call for the function in the ToggleSwitch doesn't work (the method 'checkWaitingEvents' isnt defined for the type _SWorkerActivityWidgetState.

0 Answers
Related