convertStringIntoDateTimeList function

List<DateTime?> convertStringIntoDateTimeList(
  1. String datesString
)

Implementation

List<DateTime?> convertStringIntoDateTimeList(String datesString) {
  List<DateTime?> datetimeList = [];

  //datesString.length <= 2 means the String is only "[]"
  //or empty or has 1 character (meaning definitely not a DateTime string)
  //therefore return an empty list
  //Furthermore, because the String received from the database
  //is of format "[date1, date2, ...]"
  //therefore when converting it, omit the first and last characters
  //(eg. the "[" and "]" characters of the String)
  //furthermore, each date is separated by the folowing two characters ", "
  datetimeList = datesString.length <= 2
      ? []
      : datesString
          .substring(1, datesString.length - 1)
          .split(', ')
          .map((dateString) =>
              dateString == "null" ? null : DateTime.parse(dateString))
          .toList();

  return datetimeList;
}