0

I am able to get values from a json file using flutter but since the json data is nested i want to be able to get the first parent and its nested value using an integer number my json data here

main.dat

 List<JsonModel> myModel = [];
 List<CatSubcategory> subCate = [];

loadData() async {
    var res = await http.get(url, headers: {"Accept": "application/json"});
    if (res.statusCode == 200) {
      String resBody = res.body;
      var jsonDecode = json.decode(resBody);
      for (var data in jsonDecode) {
        data['cat_subcategory'].map((x) {  
          return subCate.add(
              CatSubcategory(subName: x['sub_name'], subImage: x['sub_image']));
        }).toList();  
        myModel.add(JsonModel(
            category: data['category'],
            catId: data['cat_id'],
            catIcon: data['cat_icon'],
            catSubcategory: subCate));
        setState(() {});
      }


      int localInt = 0;
      for(var index = 0; index < myModel[localInt].catSubcategory.length; index++) {
        print(myModel[localInt].catSubcategory[index].subName);
      }


    } else {
      print("Something went wrong!");
    }

  }

instead of giving me the children of the first one which is "category": "Design & Creativity",, it will give me all of it from 0 - 3. so please how do i do this

If you require more explanation please let me know

8
  • Not sure if I understood your problem correctly, but you are mapping over every nested object inside your one json object, so it is no surprise you get more than one result? also, this question is pure json / javascript, not flutter/dart Commented Jan 28, 2021 at 13:11
  • @Leviathan well am using flutter dart for this Commented Jan 28, 2021 at 13:29
  • @Brightcode sorry not understand what you want exactly Commented Jan 28, 2021 at 13:35
  • @Shubham OK so what I want is that in my json list there are 4 like list inside it which has a category of design, legal, and 2 others and what I want is to get the Nester value of the first one which is cat_subcategory by printing it out myModel[localInt].catSubcategory[index].subName and localInt is an integer which is 0. So how do I do it Commented Jan 28, 2021 at 13:48
  • Okay so means you want to get subName, right? Commented Jan 28, 2021 at 13:53

1 Answer 1

1

First Answer

I hope this will work for you

  for(int index = 0; index < myModel[0].catSubcategory.length; index++) {
    print("==============>>${myModel[0].catSubcategory[index].subName}");
  }

Second Answer

First, you need to initialize your localInt outside of your loadData() method. And set it value 0 in your initState()

  @override
  void initState() { 
    loadData(localInt);
    localInt = 0;
    super.initState();
  }

  int localInt;

  loadData(int data) async {
    ....

    for(int index = 0; index < myModel[data].catSubcategory.length; index++) {
        print("==============>>${myModel[data].catSubcategory[index].subName}");
    }

    ...
  }
Sign up to request clarification or add additional context in comments.

6 Comments

I tried that and it works but why am using int localInt = 0; is so that I can change it when I click a button or use a dropdownbutton so it can change from 0 to 1 or 2 and get different children value
@Brightcode Ok means you are saying that when you used int localInt = 0; then it not work and in future you want to change this value on any clicks
Yeah that's what am looking for
@Brightcode I updated my answer you can check now
OK I will try it
|

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.