I am trying to change a long string text into an array, There are some methods in dart as String.split but its not working in Flutter, is there any solution that I can convert a string by spaces into an array and then use the array in a Listview
            Asked
            
        
        
            Active
            
        
            Viewed 4.7k times
        
    17
            
            
        - 
                    1`split` is working in flutter - it uses dart after all – pskink Mar 26 '19 at 12:20
 - 
                    Thanks maybe I forgot to add dart Convert package in my code – Wali Seddiqi Mar 26 '19 at 12:39
 - 
                    Convert? `split()` is a `String` method, what does Convert has to do with it? – pskink Mar 26 '19 at 12:43
 - 
                    Oh I see that, I thought its from convert class :( – Wali Seddiqi Mar 26 '19 at 13:17
 
3 Answers
28
            After using String.split to create the List (the Dart equivalent of an Array), we have a List<String>. If you wanna use the List<String> inside a ListView, you'll need a Widget which displays the text. You can simply use a Text Widget.
The following functions can help you to do this:
String.split: To split theStringto create theListList<String>.map: Map theStringto a WidgetIterable<Widget>.toList: Convert theMapback to aList
Below a quick standalone example:
import 'package:flutter/material.dart';
void main() => runApp(MyApp());
class MyApp extends StatelessWidget {
  static const String example = 'The quick brown fox jumps over the lazy dog';
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      home: Scaffold(
        body: ListView(
          children: example
              .split(' ')                       // split the text into an array
              .map((String text) => Text(text)) // put the text inside a widget
              .toList(),                        // convert the iterable to a list
        )
      ),
    );
  }
}
        NiklasPor
        
- 9,116
 - 1
 - 45
 - 47
 
- 
                    Thanks I will try that it is bit short and seems easier , I wrote it a little different – Wali Seddiqi Mar 26 '19 at 14:26
 - 
                    2static List
Calories=name.split(" "); final foods=List – Wali Seddiqi Mar 26 '19 at 14:26.generate(15,(i)=> "${Myarray[i]} ${Calories[i]}"); body: ListView.builder( itemCount: foods.length, itemBuilder: (context,index){ return ListTile( title: Text('${foods[index]}'),  - 
                    You may wanna add the code to your question, to clarify your goal and what you have tried! – NiklasPor Mar 26 '19 at 14:47
 - 
                    Okay thank you very much, I wasnt aware of being able to add code into question cuz I am new in here, I will try to add in future – Wali Seddiqi Mar 26 '19 at 16:02
 - 
                    @Niklas, this is exactly what I'm looking for, but I'm trying to split a string everything 2 characters instead of by spaces. Any suggestions? Need this for Dart as well. – Michael Apr 29 '20 at 17:42
 - 
                    1You could use RegEx and allMatches for splitting it every 2 characters. Or a simple for loop. I'd suggest you search google / whatever for some regex based solution. If you can't find any tell me again @JohnSmithly . – NiklasPor Apr 30 '20 at 08:17
 
2
            
            
        For those who want to convert each character of string into list item, we can
 String username = "John Doe";
 List<String> searchKeywords = List<String>.generate(
          username.length,
          (index) => username[index]); // ['J','o','h','n',' ','D','o','e']
Morever you can remove white spaces using trim() method
        Inder Pal Singh
        
- 380
 - 4
 - 16
 
1
            
            
        List<String> list = "Hello Word".split("");
print(list);
//[H,e,l,l,o, ,W,o,r,l,d]
List<String> list = "Hello World".split(" ");
print(list);
//["Hello", "World"]
        Aguda John Omotayo
        
- 11
 - 4
 
