Issue
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
Solution
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 theString
to create theList
List<String>.map
: Map theString
to a WidgetIterable<Widget>.toList
: Convert theMap
back 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
)
),
);
}
}
Answered By – NiklasPor
This Answer collected from stackoverflow, is licensed under cc by-sa 2.5 , cc by-sa 3.0 and cc by-sa 4.0