Home > OS >  Flutter/Dart convert a string formated as an object into a array
Flutter/Dart convert a string formated as an object into a array

Time:01-05

I am attempting to convert a string that is formatted as an object but with no luck, the data is originally formatted as a _CompactLinkedHashSet<String> so i tried to convert it to a json thinking that an array will be the output, below is my code.

var items = dataColumn[index]['items'];
final item = json.decode(json.encode(items.toString()));

When i print(items.runtimeType) the items is equals to _CompactLinkedHashSet<String> but manage to convert it to a string using json encode and decode but the output is not an array its a string formatted as below

{'Word-1', 'Word-2', 'Word-3'}

I would like to convert it to an array so i can display it inside a ListView.builder like below

['Word-1', 'Word-2', 'Word-3']

CodePudding user response:

You can change like below.

enter image description here

void main() {
  var item = {'Word-1', 'Word-2', 'Word-3'};
  print(item.runtimeType);
  print(item);
  
  // var itemList = item.map((item) => item).toList();
  var itemList = List.from(item);
  
  print(itemList.runtimeType);
  print(itemList);
}
  •  Tags:  
  • Related