30秒学会 Dart 片段 · 2019年10月10日

30秒学会 Dart 片段 – digitize

Converts an integer to a list of digits.

Use string interpolation to convert the integer to a string, String.split('') to convert it into a list.
Use Iterable.map() and int.parse() to transform each value to an integer, Iterable.toList() to return a list.

代码实现

List<int> digitze(int n) {
  return "${n}".split('').map((s) => int.parse(s)).toList();
}

使用样例

digitize(123); // [1, 2, 3]