Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to convert list<String> into String in Dart without iteration?

Tags:

string

list

dart

Is there a method in Dart like the String.join() method in Java & c#?

input:

nums: ["20",  "3005",  "2"] 

output:

nums = "2030052" 
like image 769
Haikel Avatar asked Dec 28 '17 21:12

Haikel


People also ask

How do you convert a list to a string in darts?

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);

How do you convert a list list to dynamic string in flutter?

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.


2 Answers

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 
like image 54
Mike Fahy Avatar answered Sep 28 '22 02:09

Mike Fahy


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.

like image 20
Rangana Udara Avatar answered Sep 28 '22 02:09

Rangana Udara