Reputation: 431
For example I have a list of TextSpan:
List<TextSpan> listTextSpan = [TextSpan(text: "Hello"), TextSpan(text: "World")];
then:
AutoSizeText.rich(
TextSpan(
children: listTextSpan,
)),
So I want to make the list of TextSpan as a single text, That I'll be able to return
AutoSizeText.rich(
TextSpan(
text: singleText,
)),
Upvotes: 0
Views: 470
Reputation: 5020
You can use the fold method in order to combine every item on a list into a single value, the shape of the fold method looks like this:
var result = list.fold(initial, (previous, current) => result);
So in your case, if you just want to get a string of the text of every TextSpan, you would do it like this:
String singleText = listTextSpan.fold(
'',
(prev, curr) => '$prev ${curr.text!}');
the initial value is an empty string and you take the previous value, which is the current string, and add to it a space and the text of the current TextSpan, also note that this will throw an error if one of your text spans does not have a text property, in order to avoid this, you must replace curr.text!
to curr.text ?? 'whatever you want'
If you still have issues understanding how the fold method works, you can check the documentation
Upvotes: 1