将字符串转换为颤振中的日期列表

Convert string to List of dates in flutter

我有字符串:

String datesString= "[2022-04-14 15:51:10,2022-01-29 15:51:10]";

我需要将其转换为 DateTime 或 String 列表....

我试试 JsonDecode

List datesList= jsonDecode(datesString);

它抛出格式异常:

Unhandled Exception: FormatException: Unexpected character (at character 6)

[2022-04-14 15:51:10,2022-01-29 15:51:10]

如何从字符串中获取列表?

获取字符串列表:

datesString = datesString.substring( 1, datesString.length - 1);  
var list = datesString.split(",");

要获取 DateTimes 列表,请添加到上面:

var listDates = [];
  
for (var i in list){
  listDates.add(DateTime.parse(i));
}
var date = datesString.toString().replaceAll("]", "").replaceAll("[", "");
List datesList = date.split(",");

列表将是日期列表

或者你可以这样做

 datesString = datesString.substring(1, datesString.length - 1);
List datesList = datesString.split(",");

那个字符串不是JSON。

final dates = datesString.substring(1, datesString.length - 1)
  .split(',')
  .map((date) => DateTime.parse(date));