Prashant Katoch
Prashant Katoch

Reputation: 684

How to convert String List to String in flutter?

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

Answers (7)

CrltsMrtnz
CrltsMrtnz

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

Problema

Solution

Upvotes: 0

Kamila Kalibek
Kamila Kalibek

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

Harsh Pipaliya
Harsh Pipaliya

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

amazeus
amazeus

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

nivla360
nivla360

Reputation: 1129

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

Upvotes: 8

Saik0s
Saik0s

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

Rubens Melo
Rubens Melo

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

Related Questions