Is there a method in Dart like the String.join()
method in Java & c#?
input:
nums: ["20", "3005", "2"]
output:
nums = "2030052"
Create a list named colors . List<String> colors = ['Blue', 'Red', 'Black', 'Yellow', 'White', 'Pink']; Then, convert the List colors to a JSON string using the dart:convert library's built-in jsonEncode() function. String jsonColors = jsonEncode(colors);
In dart and flutter, this example converts a list of dynamic types to a list of Strings. map() is used to iterate over a list of dynamic strings. To convert each element in the map to a String, toString() is used. Finally, use the toList() method to return a list.
join
is a method of the List class, rather than String:
List<String> yourList = ["20", "3005", "2"]; // To test that the above the above yourList.join() == '2030052'; // true yourList.join(',') == '20,3005,2'; // true, with "," delimiter
This might not be the best solution, but you can reduce a collection to a single value by iteratively combining elements of the collection using the reduce method in Dart Lists.
String nums = numsList.reduce((value, element) => value + ',' + element);
You have to remember that, the iterable must have at least one element. If it has only one element, that element is returned.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With