18

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

4
  • 1
    split is working in flutter - it uses dart after all Commented Mar 26, 2019 at 12:20
  • Thanks maybe I forgot to add dart Convert package in my code Commented Mar 26, 2019 at 12:39
  • Convert? split() is a String method, what does Convert has to do with it? Commented Mar 26, 2019 at 12:43
  • Oh I see that, I thought its from convert class :( Commented Mar 26, 2019 at 13:17

3 Answers 3

30

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
        )
      ),
    );
  }
}
Sign up to request clarification or add additional context in comments.

6 Comments

Thanks I will try that it is bit short and seems easier , I wrote it a little different
static List<String> Calories=name.split(" "); final foods=List<String>.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!
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
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 .
|
4

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

Comments

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"]

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.