0

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)

enter image description here

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"
                  )
                ],
              )
          ],
        ));
  }
}

1 Answer 1

1

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 
                     ],
                   ),
                 )
               ],
             )
         ],
       ));
 }
}
Sign up to request clarification or add additional context in comments.

1 Comment

Wow, split function is awesome! Thanks for letting me know about it xD

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.