在 flutter Dart 中附加文本

apending Text in fluttre Dart

我有一些字符串来自 API。

我想要的是将所有字符串合并在一起...

到目前为止我所做的是将所有字符串存储在一个数组中并将其转换为字符串:

    var a = List<String>();

    a.add("\n \u2022 " + "test1");
    a.add("\n \u2022 " + "test2");

结果:

[•test1
         •test2
        ]

预计:

bulleted lists without [] .

有更好的方法吗?

此代码示例应该可以回答您的问题:

void main() {
  const itemPrefix = " \u2022 ";
  
  // create a growable list of strings
  final strings = <String>[];
  
  // add some items to it
  strings.add("test1"); 
  strings.add("test2");
  
  // create a single string joining the items
  String result = strings
    // prepend the bullet point to each item
    .map((item) => "${itemPrefix}$item")
    // put a new-line between each item, joining the items to a String
    .join('\n');
  print(result);
}