How to convert String to List in Flutter

There are cases when we need to break down the string into smaller parts and store them individually.We can achieve this by simply passing the .split() function to the string.Syntax for this looks like:-

<String variable>.split(<separator>);

Here <String variable> is variable of type String and <separator> is the pattern to split the string.For example:-

String str="techstroke";
print(str.split(""));    //prints ["t","e","c","h","s","t","r","o","k","e"]

As you can see, we have converted the string to a list of individual characters using split() method.Simply pass the separating pattern into the split() funtion to get list as required.The separator can be any kind of String like a space(” “), dot(“.”),comma(“,”) etc.Another example is:-

String str="Tech Stroke";
print(str.split(" "));    //prints ["Tech","Stroke"]

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.