0

I have 2 JSON file:

json1: String people name (API)

[
  {
    "name": "Oliver"
  },
  {
    "name": "George"
  },
  {
    "name": "Harry"
  }
]

json2: String outfit and an array of people who fit that outfit (API)

[
  {
    "outfit": "T-shirt",
    "fit": [
      "Oliver",
      "George"
    ]
  },
  {
    "outfit": "Hat",
    "fit": [
      "George",
      "Harry"
    ]
  },
  {
    "outfit": "Jacket",
    "fit": [
      "Harry"
    ]
  }
]

I want that when clicking on the name of the person => show outfits that fit them

Ex. George fits a T-shirt and a hat

enter image description here

So pls help me, this is the main file:

import 'package:ask/model/page1_model.dart';
import 'package:ask/model/page2_model.dart';
import 'package:ask/services/json2_service.dart';
import 'package:ask/services/json1_service.dart';
import 'package:flutter/material.dart';

class Demo extends StatefulWidget {
  @override
  _DemoState createState() => _DemoState();
}

class _DemoState extends State<Demo> {
  List<Json1> _json1 = [];
  List<Json2> _json2 = [];

  @override
  void initState() {
    super.initState();
    Json1Services.getData().then((data) {
      setState(() {
        _json1 = data;
      });
    });
    Json2Services.getData().then((data) {
      setState(() {
        _json2 = data;
      });
    });
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
        appBar: AppBar(title: Text('List Name')),
        body: ListView.builder(
          itemCount: _json1.length,
          itemBuilder: (BuildContext context, int index) {
            Json1 json1 = _json1[index];
            return Column(children: [
              InkWell(
                  child: Text(json1.name),
                  onTap: () => Navigator.push(
                      context,
                      MaterialPageRoute(
                          builder: (context) => Scaffold(
                                appBar: AppBar(title: Text('${json1.name} fits:')),
                                body: ShowWhatFit(json2: List<Json2>.from(_json2)..retainWhere((element) => element.fit[index] == json1.name)), // I think this line is not right
                              ))))
            ]);
          },
        ));
  }
}

class ShowWhatFit extends StatelessWidget {
  final List<Json2> json2;
  ShowWhatFit({this.json2});

  @override
  Widget build(BuildContext context) {
    return Row(
      children: [
        for (int i = 0; i < json2.length; i++) Text(json2[i].outfit),
      ],
    );
  }
}

..............................................................................

3
  • @EdwynZN's answer is worth a shot. Even if that doesn't work, I'll recommend you to use a HashMap to create a third json using json1 data as keys. Commented Aug 3, 2020 at 2:11
  • EdwynZN's answer worked perfectly for me. Thanks anyway Commented Aug 3, 2020 at 8:55
  • Hi @Kel, can you share your model and service file to me, I have the similar problem to show the data as in stackoverflow.com/questions/73732881/… but not yet resolve Commented Sep 19, 2022 at 6:48

1 Answer 1

1
List<Json2>.from(_json2)..retainWhere((element) => element.fit[index] == json1.name))

retainWhere will check every element and keep only those where the condition is true. the problem is element.fit[index] == json1.name just check the element at index index of the list fit and compares it with the name json1.name, doesn't really check if the name is in the list fit. Try:

List<Json2>.from(_json2)..retainWhere((element) => element.fit.contains(json1.name)))

That will iterate every elemnt in json2, then check if the list fit contains an equal object json1.name and returns true if there is one, otherwise false

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

5 Comments

It worked correctly, you helped me to learn something new. You are my hero xD
I would recommend checking best practices for NoSQL (Firebase) or SQL and how to implement relationship tables like this example firebase.google.com/docs/database/web/structure-data, it could help you later to improve further for maintenance
Got it, it looks very useful, I will try. Thanks :">
I have a new post here, Hope you can help me implement it :D
Hi @EdwynZN, can you help me with this stackoverflow.com/questions/73732881/…

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.