3

I have this String

List<String> params = ['A','B','C'];

I want to convert this to "['A']['B']['C']" How can I convert this properly?

3 Answers 3

5

You can try:

void main(){
  List<String> params = ['A','B','C'];
  final out = params.map((e) => "['$e']").join();
  print(out);
}

Prints:

['A']['B']['C']
Sign up to request clarification or add additional context in comments.

4 Comments

My purpose is to access JSON vai field name [data/splashPage] Working solution Map jsonData = jsonDecode(snap.data ?? ""); Map<String, dynamic> splashPageJson = jsonData['data']['splashPage']; print(splashPageJson);
Not working List<String> params = ['data','splashPage']; final out = params.map((e) => "['$e']").join(); Map jsonData = jsonDecode(snap.data ?? ""); Map<String, dynamic> splashPageJson = '${jsonData}$out' as Map<String, dynamic>; print(splashPageJson); How to solve it?
@SK- It's hard to tell within the comment section. Please consider asking as a new question on Stack Oveflow.
0

you can do this

  List<String> params = ['A','B','C'];
  List newParams = [];
  for(var item in params){
    newParams.add([item]);
  }
  String stringParams = newParams.toString();
  String noBracketParams = stringParams.substring( 1, stringParams.length - 1 );
  String noCommasParams = noBracketParams.replaceAll(',', '');
  print(noCommasParams);

Comments

0
  • I'm not sure what you're trying to do, but it can be achieved like this
    List<String> params = ['A', 'B', 'C'];
    List.generate(
        params.length, (index) => params[index] = '''['${params[index]}']''');
    var str = '';
    params.forEach((item) => str += item);
    print(str);

1 Comment

// ['A']['B']['C']

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.