Question in dart:
How can I convert a String like this:
String
"[137, 80, 78, 71, 13, 10, 26, 10]"
to type List<Int>
List<Int>
Thanks
3
I did it like this:
json.decode()
Meaning....
String value = "[137, 80, 78, 71, 13, 10, 26, 10]" List<int> list = json.decode(value).cast<int>();
You can use RegExp like this:
RegExp
String str = "[137, 80, 78, 71, 13, 10, 26, 10]"; List<int> intList = str.replaceAll(RegExp(r'[[], ]'), '').split('').map(int.parse).toList(); print(intList);
happy coding…
Follow the below steps.
Remove the ‘[]’
Use split() method
split()
Turn it to an int List
List<int> list = value.replaceAll('[', '') .replaceAll(']', '').split(',') .map<int>((e) { return int.parse(e); }).toList(); print(list);// [137, 80, 78, 71, 13, 10, 26, 10]
Click here to cancel reply.
3
Answers
I did it like this:
Meaning....
You can use
RegExp
like this:happy coding…
Follow the below steps.
Remove the ‘[]’
Use
split()
methodTurn it to an int List