51

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(","); }); 
1
  • 1
    you can iterate a list and concatenate items with stringbuffer Commented Jun 4, 2019 at 11:56

7 Answers 7

108

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("")); 
Sign up to request clarification or add additional context in comments.

1 Comment

How can I make it clickable for each one of them?
22

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' } 

Comments

21

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); 

1 Comment

I am getting the following error when using a list of class (List<ClassName> classList) The operator '+' isn't defined for the type 'ClassName'.
9

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(","); 

Comments

8
 print(list.toString().replaceAll('[', '').replaceAll(']','')) 

Comments

0

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)), ], ), ); } }

Comments

0

@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(']', ''), 

Problema

Solution

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.