1

How to regenerate whole list to convert string(list) inside of the list into list format

 List<Map<String, dynamic>> category = [
    {
      "name": "One",
      "detail": "['1', '1', '1', '1', '1', '1']"
    },
    {
      "name": "two",
      "detail": "['2', '2', '2', '2', '2', '2']"
    },
    {
      "name": "three",
      "detail": "['3', '3', '3', '3', '3', '3']"
    },
  ];


Become

 List<Map<String, dynamic>> category = [
    {
      "name": "One",
      "detail": ['1', '1', '1', '1', '1', '1']
    },
    {
      "name": "two",
      "detail": ['2', '2', '2', '2', '2', '2']
    },
    {
      "name": "three",
      "detail": ['3', '3', '3', '3', '3', '3']
    },
  ];

How to regenerate whole list to convert string(list) inside of the list into list format

Edward
  • 89
  • 1
  • 6
  • Does this answer your question? [Dart: Convert String representation of List of Lists to List of List](https://stackoverflow.com/questions/43810508/dart-convert-string-representation-of-list-of-lists-to-list-of-list) – OMi Shah Nov 04 '22 at 17:38

3 Answers3

0
List<Map<String, dynamic>> category = [
    {
      "name": "One",
      "detail": "['1', '1', '1', '1', '1', '1']"
    },
    {
      "name": "two",
      "detail": "['2', '2', '2', '2', '2', '2']"
    },
    {
      "name": "three",
      "detail": "['3', '3', '3', '3', '3', '3']"
    },
  ];


for(var e in category){
    Map<String,dynamic> item = e;
    String s = e['detail'];
    s = s.substring(1,s.length-1);
    item['detail'] = s.split(',');
    result.add(item);
  }

print(result);
Naveen Avidi
  • 3,004
  • 1
  • 11
  • 23
0

There are more ways of doing this :

try running this example on dartpad

import 'dart:convert';

void main() {
  String ss = "['3', '3', '3', '3', '3', '3']";
  String a= ss.replaceAll('[','');
  String b= a.replaceAll(']','');
  String c= b.replaceAll("'",'');
  List result = c.split(',');
  print(result);
}
MANISH DAYMA
  • 1,126
  • 3
  • 18
0
for (var element in category) {
  element['detail'] = json.decode(element['detail'].toString().replaceAll("\'", "\""));
}
log("$category");
Tyler2P
  • 2,324
  • 26
  • 22
  • 31
Ninad7N
  • 544
  • 4
  • 13
  • 2
    Your answer could be improved by adding more information on what the code does and how it helps the OP. – Tyler2P Nov 04 '22 at 17:22
  • [A code-only answer is not high quality](//meta.stackoverflow.com/questions/392712/explaining-entirely-code-based-answers). While this code may be useful, you can improve it by saying why it works, how it works, when it should be used, and what its limitations are. Please [edit] your answer to include explanation and link to relevant documentation. – ray Nov 06 '22 at 01:55