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(","); });
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.
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.
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.
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(""));
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.
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