skip to Main Content

Question in dart:

How can I convert a String like this:

"[137, 80, 78, 71, 13, 10, 26, 10]"

to type List<Int>

Thanks

3

Answers


  1. Chosen as BEST ANSWER

    I did it like this:

    json.decode() 
    

    Meaning....

    String value = "[137, 80, 78, 71, 13, 10, 26, 10]"
    
    List<int> list = json.decode(value).cast<int>();
    

  2. You can use RegExp like this:

     String str = "[137, 80, 78, 71, 13, 10, 26, 10]";
      
      List<int> intList = str.replaceAll(RegExp(r'[[], ]'), '').split('').map(int.parse).toList();
      
      print(intList);
    
    

    happy coding…

    Login or Signup to reply.
  3. Follow the below steps.

    1. Remove the ‘[]’

    2. Use split() method

    3. Turn it to an int List

       List<int> list = value.replaceAll('[', '')
                        .replaceAll(']', '').split(',')
                        .map<int>((e) { 
                        return int.parse(e);
                        }).toList();
      
       print(list);// [137, 80, 78, 71, 13, 10, 26, 10]
      
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search