17

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

Wali Seddiqi
  • 436
  • 1
  • 5
  • 10

3 Answers3

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:

  1. String.split: To split the String to create the List
  2. List<String>.map: Map the String to a Widget
  3. Iterable<Widget>.toList: Convert the Map back to a List

Below a quick standalone example:

Code 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
  • 2
    static List Calories=name.split(" "); final foods=List.generate(15,(i)=> "${Myarray[i]} ${Calories[i]}"); body: ListView.builder( itemCount: foods.length, itemBuilder: (context,index){ return ListTile( title: Text('${foods[index]}'), – Wali Seddiqi Mar 26 '19 at 14:26
  • 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
  • 1
    You 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"]