Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to convert String List to String in flutter?

Tags:

I need to convert List<String> into a String in the dart.

I want to extract the value of the list from preferences. I have tried this implementation but it is only giving me the last value.

Future<List<String>> services = SharedPrefSignUp.getSelectedServices(); services.then((onValue){   List<String>servicesList=onValue;   selectServicesText=servicesList.join(","); }); 
like image 204
Prashant Katoch Avatar asked Jun 04 '19 11:06

Prashant Katoch


People also ask

How do I convert a list to a string in flutter?

Using String join() Dart's List class has an instance method join() that can be used to join a List of any type to a String . Below is a simple example in which we are going to join a list of strings into a string. Without passing the separator parameter, the default separator (empty string) will be used.

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.

How do I turn a list into a string?

To convert a list to a string, use Python List Comprehension and the join() function. The list comprehension will traverse the elements one by one, and the join() method will concatenate the list's elements into a new string and return it as output.


2 Answers

if you know you have List<String> then you can use join() function provided by a flutter.

    var list = ['one', 'two', 'three'];     var stringList = list.join("");     print(stringList); //Prints "onetwothree" 

Simple and short. ;)

And you can use it like this:

 List<String> servicesList = ["one", "Two", "Thee"];   print(servicesList.join("")); 
like image 186
Harsh Pipaliya Avatar answered Sep 23 '22 01:09

Harsh Pipaliya


If you want to convert your List<String> to a coma septated String, you can do this

 List<String> list =["one", "Two", "Thee"];  print(list.join(","));  // Output will be like this : one,Two,Thee 

Join() method Converts each element to a String and concatenates the strings.

like image 44
Paresh Mangukiya Avatar answered Sep 20 '22 01:09

Paresh Mangukiya