Q:

How to split String Data into array from JSON

I have a JSON file like this (API)
[
  {
    "area": "Area A",
    "country": [
      "United States:1",
      "Canada:1",
      "Germany:2"
    ]
  },
  {
    "area": "Area B",
    "country": [
      "United Kingdom:2",
      "Mexico:1",
      "Denmark:2"
    ]
  }
]

in array country, I want to split the inner String separated by characters ":" into an array
Ex. "United States:1" => ["United States",1]
Assuming this new array data is countrySplit, then I could access it with countrySplit[0] (show "United States") and countrySplit[1] (show 1)

this is the main file
import 'package:ask/model/split_data_model.dart';
import 'package:ask/services/split_data_service.dart';
import 'package:flutter/material.dart';

class SplitDataPage extends StatefulWidget {
  SplitDataPage() : super();
  @override
  _SplitDataPageState createState() => _SplitDataPageState();
}

class _SplitDataPageState extends State<SplitDataPage> {
  List<SplitData> _split = [];

  @override
  void initState() {
    super.initState();
    SplitDataService.getSplitData().then((split) {
      setState(() {
        _split = split;
      });
    });
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
        appBar: AppBar(title: Text('Split Data')),
        body: Column(
          children: [
            for (int i = 0; i < _split.length; i++)
              Row(
                children: [
                  Expanded(child: Text(_split[i].area)),
                  Expanded(
                    child: Column(
                      children: [
                        for (String countries in _split[i].country) Text(countries), // How to split String country into array Ex: countrySplit[0] -> "United States"
                      ],
                    ),
                  ),
                  Expanded(
                    child: Container(), // How to split String country into array Ex: countrySplit[1] -> "1"
                  )
                ],
              )
          ],
        ));
  }
}

A:

you can simply use split function to split data with separator :
import 'package:ask/model/split_data_model.dart';
import 'package:ask/services/split_data_service.dart';
import 'package:flutter/material.dart';

class SplitDataPage extends StatefulWidget {
 SplitDataPage() : super();
 @override
 _SplitDataPageState createState() => _SplitDataPageState();
}

class _SplitDataPageState extends State<SplitDataPage> {
 List<SplitData> _split = [];

 @override
 void initState() {
   super.initState();
   SplitDataService.getSplitData().then((split) {
     setState(() {
       _split = split;
     });
   });
 }

 @override
 Widget build(BuildContext context) {
   return Scaffold(
       appBar: AppBar(title: Text('Split Data')),
       body: Column(
         children: [
           for (int i = 0; i < _split.length; i++)
             Row(
               children: [
                 Expanded(child: Text(_split[i].area)),
                 Expanded(
                   child: Column(
                     children: [
                       for (String countries in _split[i].country) Text(countries.split(':')[0]), //print country name
                     ],
                   ),
                 ),
                 Expanded(
                   child: Column(
                     children: [
                       for (String countries in _split[i].country) Text(countries.split(':')[1]), //print number 
                     ],
                   ),
                 )
               ],
             )
         ],
       ));
 }
}

