[英]How to convert String List to String in flutter?
I need to convert List<String>
into a String
in the dart.我需要将
List<String>
转换为 dart 中的String
。
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(",");
});
if you know you have List<String>
then you can use join()
function provided by a flutter.如果你知道你有
List<String>
那么你可以使用 flutter 提供的join()
函数。
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(""));
You can iterate list and concatenate values with StringBuffer您可以使用 StringBuffer 迭代列表和连接值
var list = ['one', 'two', 'three'];
var concatenate = StringBuffer();
list.forEach((item){
concatenate.write(item);
});
print(concatenate); // displays 'onetwothree'
}
You can use reduce method on a list like that:您可以在这样的列表上使用reduce方法:
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);
If you want to convert your List<String>
to a coma septated String
, you can do this如果要将
List<String>
转换为coma septated String
,则可以执行此操作
List<String> list =["one", "Two", "Thee"];
print(list.join(",")); // Output will be like this : one,Two,Thee
Join() method Converts each element to a String and concatenates the strings. Join()方法将每个元素转换为字符串并连接字符串。
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(",");
print(list.toString().replaceAll('[', '').replaceAll(']',''))
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)), ], ), ); } }
@nivia360 its work for me! @nivia360 它为我工作!
The problem is cant eliminate the bracket to the list of array.问题是无法消除数组列表中的括号。 this code
movie.tags.toString(),
Return this list whit brackets此代码
movie.tags.toString(),
返回此列表带括号[Adventure, Fantasy, Action]
I soved how the @nivia360 propouse usingn我解决了@nivia360 如何使用
movie.tags.toString().replaceAll('[', '').replaceAll(']', ''),
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.