2

I have a list. I want to convert it to string. However, the results are a bit different from what I expected.

Expected result:

'["a", "b", "c", "d"]'

Actual result:

[a, b, c, d]

My code:

final List list = ["a", "b", "c", "d"];

print(list.toString());

How can I get my expected results? I would appreciate any help. Thank you in advance!

4
  • 1
    @eamirho3ein I don't understand why you said "looks like regular word" Commented Nov 7, 2022 at 9:44
  • @eamirho3ein are you saying that when I use print('"a"'); it returns the result a? Commented Nov 7, 2022 at 9:48
  • @eamirho3ein yes you are right Commented Nov 7, 2022 at 9:51
  • 1
    @eamirho3ein can you see this question and its answer, in this answer I want to change the color of children text when I click Commented Nov 7, 2022 at 10:22

3 Answers 3

2

You could use the jsonEncode of it to get the desired result:

print(jsonEncode(list));
Sign up to request clarification or add additional context in comments.

6 Comments

this would convert the list to string.
@eamirho3ein of course... read the title of the question
Hi @Ivo, this solved my problem! I can't accept it now, I'll accept it in 6 minutes
I assumed his question was about the way string prints. now make sense.
@Ivo I accepted your answer :)
|
0

You can try jsonEncode:

final List list = ["a", "b", "c", "d"];

print(jsonEncode(list));

Output:

["a","b","c","d"]

Comments

0

You can simply do this by using json.encode() function:

final list = ['a', 'b', 'c', 'd'];
final result = json.encode(list);

1 Comment

Your answer could be improved with additional supporting information. Please edit to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers in the help center.

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.