I have String :
String datesString= "[2022-04-14 15:51:10,2022-01-29 15:51:10]";
I need to convert it to list of DateTime or String ....
I try JsonDecode
List datesList= jsonDecode(datesString);
it throw a format exception :
Unhandled Exception: FormatException: Unexpected character (at character 6)
[2022-04-14 15:51:10,2022-01-29 15:51:10]
how I can get the list from the String ?
CodePudding user response:
That string is not JSON.
final dates = datesString.substring(1, datesString.length - 1)
.split(',')
.map((date) => DateTime.parse(date));
CodePudding user response:
var date = datesString.toString().replaceAll("]", "").replaceAll("[", "");
List datesList = date.split(",");
List will be datesList
or you can do like this
datesString = datesString.substring(1, datesString.length - 1);
List datesList = datesString.split(",");
CodePudding user response:
To get a list of Strings :
datesString = datesString.substring( 1, datesString.length - 1);
var list = datesString.split(",");
And to get a list of DateTimes, add to the above :
var listDates = [];
for (var i in list){
listDates.add(DateTime.parse(i));
}