Ad

Apending Text In Fluttre Dart

- 1 answer

I have number of Strings coming from an API.

What I want is to merge all Strings together...

What I've done so far is store all Strings in an Array and convert that to a String:

    var a = List<String>();

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

Result:

[•test1
         •test2
        ]

Expected:

bulleted lists without [] .

Is there a better way to do this?

Ad

Answer

This code sample should answer your questions:

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);
}
Ad
source: stackoverflow.com
Ad