0

I have problem and dont know to solve.

List<int> list= [1, 2, 3];

How to convert list above into this:

String data= '[1, 2, 3]';

3
  • Does this answer your question? Convert a List<int> into a String in Dart? Commented May 15, 2020 at 10:56
  • @RavindraKushwaha in my case, i need to store my list<int> into txt file. Its require me using String to store Commented May 15, 2020 at 13:14
  • @VirenVVarasadiya I didn't know this worked well, thanks! Commented May 15, 2020 at 13:18

3 Answers 3

4

You can use toString() metohd.

 String data =  list.toString();
Sign up to request clarification or add additional context in comments.

Comments

1

please try this code

List<int> list= [1, 2, 3];String data  = list.toString().replaceAll('[', "'[").replaceAll(']',"]'");print(data);

you can try it on the dart pad to know the solution

1 Comment

Sneha G Sneha, I didn't realize that this also works fine, thanks!
0

Another way. Independent of the built-in 'toString()'.

void main() {
  final list = [1, 2, 3];
  final data = '[${list.join(', ')}]';
  print(data);
}

Comments

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.