[英]Flutter: How to display data from 2 JSON based on the same value of Array
I have 2 JSON file:我有 2 JSON 文件:
json1: String people name ( API ) json1:字符串人名( API )
[
{
"name": "Oliver"
},
{
"name": "George"
},
{
"name": "Harry"
}
]
json2: String outfit and an array of people who fit that outfit ( API ) json2:字符串装备和一组适合该装备的人( 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
乔治适合一件 T 恤和一顶帽子
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),
],
);
}
}
.............................................................................. ..................................................... .....................................
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. retainWhere
将检查每个元素并仅保留条件为真的元素。 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
.问题是
element.fit[index] == json1.name
只是检查列表fit
的索引index
处的元素并将其与名称json1.name
进行比较,并没有真正检查名称是否在列表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这将迭代 json2 中的每个元素,然后检查列表是否包含一个相等的
fit
json1.name
,如果存在则返回 true,否则返回 false
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.