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
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 the String
to create the List
List<String>.map
: Map the String
to a WidgetIterable<Widget>.toList
: Convert the Map
back to a List
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
)
),
);
}
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With