My Car
My Car

Reputation: 4566

How to convert list to string in Flutter?

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!

Upvotes: 2

Views: 1255

Answers (3)

Sparko Sol
Sparko Sol

Reputation: 717

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

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

Upvotes: 0

Sachin Kumawat
Sachin Kumawat

Reputation: 445

You can try jsonEncode:

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

print(jsonEncode(list));

Output:

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

Upvotes: 0

Ivo
Ivo

Reputation: 23357

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

print(jsonEncode(list));

Upvotes: 2

Related Questions