0

I need to create 96 Map objects in a List with the following key-value pairs

{
    'id': 1, 
    'time': '00:00-00:15', 
    'slotNumber': '01', 
    'clicked': false
}

Although this is something that is easily achievable using loops, the main complication starts when it comes to generating the time range. The time key in every object needs to be at an interval of 15 minutes each and should be in a 24-hour format. For example, the next few time ranges need to be 00:15-00:30, 00:30-00:45, 00:45-01:00, and so on. I tried hard to look for a package that would answer all my prayers but couldn't find any.

The final output needs to look something like this:

var kSlots = [
  {'id': 1, 'time': '00:00-00:15', 'slotNumber': '01', 'clicked': false},
  {'id': 2, 'time': '00:15-00:30', 'slotNumber': '02', 'clicked': false},
  {'id': 3, 'time': '00:45-01:00', 'slotNumber': '03', 'clicked': false},
  {'id': 4, 'time': '01:00-01:15', 'slotNumber': '04', 'clicked': false},
  {'id': 5, 'time': '01:15-01:30', 'slotNumber': '05', 'clicked': false},
  {'id': 6, 'time': '01:30-01:45', 'slotNumber': '06', 'clicked': false},
  {'id': 7, 'time': '01:45-02:00', 'slotNumber': '07', 'clicked': false},
  {'id': 8, 'time': '02:00-02:15', 'slotNumber': '08', 'clicked': false}]

As I now need to generate the time from a given start date which is in String, I tried modifying @jamesdlin 's answer a bit to achieve that. But it throws the following error:

Uncaught Error: FormatException: Invalid date format
08:00

String getTimeRange(int i) {
  var midnight = DateTime.parse(DateFormat('HH:mm').format(DateTime.parse('08:00')));
  const interval = Duration(minutes: 15);
  var start = midnight.add(interval * i);
  var end = start.add(interval);
  var formatTime = DateFormat('HH:mm').format;
  return '${formatTime(start)}-${formatTime(end)}';
  
}

void main() {
  var slots = [
    for (var i = 0; i < 20; i += 1)
    <String, dynamic>{
      'id': i + 1,
      'time': getTimeRange(i),
      'slotNumber': '${i + 1}'.padLeft(2, '0'),
      'clicked': false,
    },
  ];
  
  slots.forEach(print);
}
coolhack7
  • 1,204
  • 16
  • 35
  • Regarding your edit: Just construct a `DateTime` object directly with the time you want: `DateTime(2022, 1, 1, 8)`. Also don't name that variable `midnight`. – jamesdlin Oct 04 '22 at 16:40
  • And what if the time was in HH:mm format? How would you suggest I used something like 08:45 in place of 8? – coolhack7 Oct 05 '22 at 04:08
  • 1
    If the time is known, then you can still use the `DateTime` constructor (`DateTime(2022, 1, 1, 8, 45)`). If you need to parse a string dynamically, then you would need to use `DateFormat('HH:mm').parse`, not `DateTime.parse`. See https://stackoverflow.com/a/61394854/ – jamesdlin Oct 05 '22 at 04:44

2 Answers2

1

Loop over an index and use the index as a multiplier for a Duration of 15 minutes.

You then either can add that multiplied Duration to a DateTime representing midnight (if your time ranges represent points in time) or format those Durations directly (if they represent durations).

I assuming that your times represent points in time:

import 'package:intl/intl.dart';

String getTimeRange(int i) {
  var midnight = DateTime.utc(2022, 1, 1);
  const interval = Duration(minutes: 15);
  var start = midnight.add(interval * i);
  var end = start.add(interval);
  var formatTime = DateFormat('HH:mm').format;
  return '${formatTime(start)}-${formatTime(end)}';
  
}

void main() {
  var slots = [
    for (var i = 0; i < 8; i += 1)
    <String, dynamic>{
      'id': i + 1,
      'time': getTimeRange(i),
      'slotNumber': '${i + 1}'.padLeft(2, '0'),
      'clicked': false,
    },
  ];
  
  slots.forEach(print);
}
jamesdlin
  • 81,374
  • 13
  • 159
  • 204
  • What do I need to change to start calculating from a particular time? Like let's say If I wanted to start generating the time slots from 08:00? – coolhack7 Oct 04 '22 at 14:59
  • @coolhack7 Then use 08:00 as the base time instead of midnight. – jamesdlin Oct 04 '22 at 15:26
  • I have kind of tried that but it didn't work which probably is due to me going wrong somewhere. I have therefore updated the question with what I have done above. Do you think you can go through it once and point out what I have done wrong? – coolhack7 Oct 04 '22 at 15:39
0

You can set use the DateTime class instead of String and manipulate it as you desire: https://api.flutter.dev/flutter/dart-core/DateTime-class.html

Răzvan Puiu
  • 671
  • 1
  • 6
  • 24