Reputation: 684
I need to convert List<String>
into a String
in the dart.
I want to extract the value of the list from preferences. I have tried this implementation but it is only giving me the last value.
Future<List<String>> services = SharedPrefSignUp.getSelectedServices();
services.then((onValue){
List<String>servicesList=onValue;
selectServicesText=servicesList.join(",");
});
Upvotes: 51
Views: 122307
Reputation: 59
@nivia360 its work for me!
The problem is cant eliminate the bracket to the list of array.
this code movie.tags.toString(),
Return this list whit brackets
[Adventure, Fantasy, Action]
I soved how the @nivia360 propouse usingn
movie.tags.toString().replaceAll('[', '').replaceAll(']', ''),
Upvotes: 0
Reputation: 1
if you need just first word to be highlighted use this
import 'package:flutter/material.dart';
class TextHighlight extends StatefulWidget {
final String text;
TextHighlight({
required this.text,
});
@override
State<TextHighlight> createState() => _textHighlightState();
}
class _textHighlightState extends State<TextHighlight> {
String first = '';
List<String> allText = [];
List<String> newText = [];
@override
Widget build(BuildContext context) {
first = widget.text.split(' ').first;
allText = widget.text.split(' ')..removeAt(0);
return RichText(
text: TextSpan(
children: [
TextSpan(text: first+' ', style: TextStyle(color: Colors.green, fontSize: 17)),
TextSpan(text: allText.join(' ').toString(), style: TextStyle(color: Colors.black)),
],
),
);
}
}
Upvotes: 0
Reputation: 2431
if you know you have List<String>
then you can use join()
function provided by a flutter.
var list = ['one', 'two', 'three'];
var stringList = list.join("");
print(stringList); //Prints "onetwothree"
Simple and short. ;)
And you can use it like this:
List<String> servicesList = ["one", "Two", "Thee"];
print(servicesList.join(""));
Upvotes: 107
Reputation: 136
Just in case someone wants to concatenate specific member strings in a list of objects : here is a way to do the same-
string commaSeparatedNames=return listOfObjectsWithNameAttribute
.map((item) => item.name)
.toList()
.join(",");
Upvotes: 9
Reputation: 446
You can use reduce method on a list like that:
List<String> list = ['one', 'two', 'three'];
final string = list.reduce((value, element) => value + ',' + element);
// For your example:
List<String> servicesList = await SharedPrefSignUp.getSelectedServices();
selectServicesText = servicesList.reduce((value, element) => value + ',' + element);
Upvotes: 21
Reputation: 3315
You can iterate list and concatenate values with StringBuffer
var list = ['one', 'two', 'three'];
var concatenate = StringBuffer();
list.forEach((item){
concatenate.write(item);
});
print(concatenate); // displays 'onetwothree'
}
Upvotes: 22