Home > Enterprise >  Flutter how to loop through multiple lists
Flutter how to loop through multiple lists

Time:12-16

I am using this code to loop through a list in flutter:

if (notificationDays.isNotEmpty) {
          for (var i = 0; i < selectedNotificationDays.length; i  ) {
            createWaterReminderNotification(
                'Reminder',
                NotificationWeekAndTime(
                    dayOfTheWeek: int.parse(selectedNotificationDays[i]),
                    timeOfDay: remindertime));
          }
        }

But now I want to replace 'remindertime' with a list instead. What can I do to loop through every item in this list? I have tried this but it doesn't work:

if (notificationDays.isNotEmpty) {
          for (var i = 0; i < selectedNotificationDays.length; i  ) {
            createWaterReminderNotification(
                'Reminder',
                NotificationWeekAndTime(
                    dayOfTheWeek: int.parse(selectedNotificationDays[i]),
                    timeOfDay: remindertime[i]));
          }
        }

'remindertime' is now a list of multiple 'timeOfDay'. So, I got one list with days called 'selectedNotificationDays'. and one list with times called 'remindertime'.

I want to create a reminder on every day of selectedNotificationDays. and on that day I want to create this reminder on every time of reminder time.

like this: Monday: (10.00 12.30 15.00) Tuesday: (10.00 12.30 15.00) Friday: (10.00 12.30 15.00)

CodePudding user response:

You would need to use another for loop. I also recommend replacing it with a for in for better readability. This is how you do it:

for(var selectedDay in selectedNotificationDays){
  for(var reminder in remindertime){
    createWaterReminderNotification(
      'Reminder',
      NotificationWeekAndTime(
        dayOfTheWeek: int.parse(selectedDay),
        timeOfDay: reminder,
      ),
    );
  }
}
  • Related