47

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 Jun 4, 2019 at 11:56

7 Answers 7

99

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(""));
1
  • How can I make it clickable for each one of them? Jul 22, 2020 at 21:17
21

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'

  }
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
  • I am getting the following error when using a list of class (List<ClassName> classList) The operator '+' isn't defined for the type 'ClassName'. Feb 7, 2022 at 8:01
8
 print(list.toString().replaceAll('[', '').replaceAll(']',''))
1
  • 1
    Thanks. This is a good approach as well. Apr 21, 2022 at 13:59
7

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

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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.