{
  "id": 3233136,
  "title": "Q&D: String and Iterable in Dart",
  "url": "https://urgent.news/2026/08/25/q-d-string-and-iterable-in-dart",
  "topic": "tech",
  "section": "Tech",
  "published": "2026-08-25T10:00:00.000Z",
  "source": {
    "name": "Dev.to",
    "slug": "dev-to",
    "url": "https://dev.to/niamtokik/qd-string-and-iterable-in-dart-4ac5"
  },
  "original_language": "en",
  "account": "In Dart, a String cannot be used directly as an Iterable. To convert a String into a List or another Iterable class, one must import the dart:convert module and utilize its converters. For a pure ASCII string, the ascii.encode(str) method can be employed, where str is the String to be converted to List<int>. The following Dart code demonstrates this:\n\nimport 'dart:convert';\n\nvoid main() {\nfinal str = 'hello world';\nfor (var c in ascii.encode(str)) {\nprint('$c (${String.fromCharCode(c)})');\n}\n}\n\nWhen run, this code outputs each character of the string along with its ASCII value: 104 (h), 101 (e), 108 (l), 108 (l), 111 (o), 32 ( ), 119 (w), 111 (o), 114 (r), 108 (l), and 100 (d).\n\nSimilarly, an utf8 String can also be converted into an Iterable. Consider the following Dart code:\n\nimport 'dart:convert';\n\nvoid main() {\nfinal str = '波動拳: ↓↙←Ⓑ';\nfor (var c in utf8.encode(str)) {\nprint('$c (${String.fromCharCode(c)})');\n}\n}\n\nUpon execution, this code will display the character along with its UTF-8 encoded value. For instance, the first character '波' is represented as 230 (æ), 179 (³), and 162 (¢). The output appears normal because an utf8 character is encoded in 8, 16, or 32 bits, but the encoder outputs the characters only in 8 bits. This results in an utf8 character being split across multiple bytes.\n\nTo avoid this, one can use the String.split() method to generate a List and iterate over it. The following Dart code accomplishes this:\n\nvoid main() {\nfinal str = '波動拳: ↓↙←Ⓑ';\nfor (var c in str.split('')) {\nprint('$c : ${utf8.encode(c)}');\n}\n}\n\nThe output of this code is as follows: '波 : [230, 179, 162]', '動 : [229, 139, 149]', '拳 : [230, 139, 179]', ':' : [58], ' ' : [32], '↓ : [226, 134, 147]', '↙ : [226, 134, 153]', '← : [226, 134, 144]', 'Ⓑ : [226, 146, 183]'. Each Unicode character is represented by its corresponding integers.",
  "summary": "By default, String s in Dart can't be used as Iterable , they must be converted to a List<int> or another kind of Iterable class. To do that, one must import the dart:convert module and start playing with the converters. A pure ASCII string can be converted using ascii.encode(str) , where str is the String to convert to List<int> . import 'dart:convert' ; void main () { final str = \"hello world\"…",
  "key_points": [
    "String cannot be directly used as Iterable in Dart",
    "Use dart:convert module to convert String to List or Iterable",
    "ascii.encode() method converts pure ASCII string to List int"
  ],
  "editors_take": null,
  "illustration": null,
  "coverage": {
    "outlets": 1,
    "also_reported_by": []
  },
  "ai_generated": true,
  "disclaimer": "Summaries, key points and the editor’s take are written by software from other outlets’ reporting and may contain errors — always check the linked original."
}