从其他数组访问 JSON 数组值 - flutter
Access JSON array value from other array - flutter
我想做的是,如果有人打开第 1 部分 (JSON),那么他们各自的文章应该会出现在下一个屏幕上。我能够列出所有部分,但如何在下一个屏幕上显示它们各自的文章。例如,第一部分包含 3 篇文章,第二部分将包含 3
基本上,每个部分都会包含不同数量的文章。
JSON
[
[ // Article Names
{
"ArtNo": "0",
"Name": "PREAMBLE",
},
{
"ArtNo": "1",
"Name": "Name and territory of the Union.",
},
{
"ArtNo": "2",
"Name": "Admission or establishment of new States.",
},
{
"ArtNo": "3",
"Name": "Formation of new States and alteration of areas, boundaries or names of existing States.",
},
{
"ArtNo": "4",
"Name": "Laws made under articles 2 and 3 to provide for the amendment of the First and the Fourth Schedules and supplemental, incidental and consequential matters.",
},
{
"ArtNo": "5",
"Name": "Citizenship at the commencement of the Constitution."
}
],
[ // Article Parts
{
"PartNo": "I",
"Name": "THE UNION AND ITS TERRITORY",
"Articles": ["1", "2", "3"]
},
{
"PartNo": "II",
"Name": "CITIZENSHIP",
"Articles": ["4", "5"]
}
]
]
Flutter(列出所有部分)
class _ConstitutionPartsState extends State<ConstitutionParts> {
Future parts() async {
final data = await rootBundle.loadString('assets/json/file.json');
final jsonResult = jsonDecode(data);
final parts = jsonResult[1];
return parts;
}
@override
Widget build(BuildContext context) {
return Scaffold(
body: FutureBuilder(
future: parts(), builder: (BuildContext context, AsyncSnapshot<dynamic> snapshot) {
if(snapshot.connectionState == ConnectionState.waiting){
return const CircularProgressIndicator(color: Colors.deepOrangeAccent);
}
return Container(
margin: const EdgeInsets.all(25.0),
child: ListView.builder(
itemCount: snapshot.data.length,
itemBuilder: (BuildContext context, int index) {
return Container(
margin: const EdgeInsets.only(bottom: 20.0),
child: ListTile(
title: Text(snapshot.data[index]['Name']),
),
);
},
),
);
},
),
);
}
}
如有任何帮助,我们将不胜感激!谢谢
可以使用where
条件进行筛选,
这是完整代码
class _ConstitutionPartsState extends State<ConstitutionParts> {
Future parts() async {
final data = await rootBundle.loadString('assets/json/file.json');
final jsonResult = jsonDecode(data);
return jsonResult; // return whole json
}
@override
Widget build(BuildContext context) {
return Scaffold(
body: FutureBuilder(
future: parts(), builder: (BuildContext context, AsyncSnapshot<dynamic> snapshot) {
if(snapshot.connectionState == ConnectionState.waiting){
return const CircularProgressIndicator(color: Colors.deepOrangeAccent);
}
return Container(
margin: const EdgeInsets.all(25.0),
child: ListView.builder(
itemCount: snapshot.data[1].length,
itemBuilder: (BuildContext context, int index) {
return Container(
margin: const EdgeInsets.only(bottom: 20.0),
child: ListTile(
title: Text(snapshot.data[index]['Name']),
onTap():{
//navigate to name page with
NewPage(articePartIndex : index, json: snapshot.data)
}
),
);
},
),
);
},
),
);
}
}
NewPage {
final List<List<dynamic>> json;
final int index;
@override
Widget build(BuildContext context) {
var articles = json[1][index]['Articles'];
var filteredArticleList = json[0].where((a) => articles.contains(a['ArtNo'])).toList();
return ListView.builder(
itemCount: snapshot.data.length,
itemBuilder: (BuildContext context, int index) {
return ListTile(
title: Text(filteredArticleList[index]['Name']),
),
});
}
}
我想做的是,如果有人打开第 1 部分 (JSON),那么他们各自的文章应该会出现在下一个屏幕上。我能够列出所有部分,但如何在下一个屏幕上显示它们各自的文章。例如,第一部分包含 3 篇文章,第二部分将包含 3
基本上,每个部分都会包含不同数量的文章。
JSON
[
[ // Article Names
{
"ArtNo": "0",
"Name": "PREAMBLE",
},
{
"ArtNo": "1",
"Name": "Name and territory of the Union.",
},
{
"ArtNo": "2",
"Name": "Admission or establishment of new States.",
},
{
"ArtNo": "3",
"Name": "Formation of new States and alteration of areas, boundaries or names of existing States.",
},
{
"ArtNo": "4",
"Name": "Laws made under articles 2 and 3 to provide for the amendment of the First and the Fourth Schedules and supplemental, incidental and consequential matters.",
},
{
"ArtNo": "5",
"Name": "Citizenship at the commencement of the Constitution."
}
],
[ // Article Parts
{
"PartNo": "I",
"Name": "THE UNION AND ITS TERRITORY",
"Articles": ["1", "2", "3"]
},
{
"PartNo": "II",
"Name": "CITIZENSHIP",
"Articles": ["4", "5"]
}
]
]
Flutter(列出所有部分)
class _ConstitutionPartsState extends State<ConstitutionParts> {
Future parts() async {
final data = await rootBundle.loadString('assets/json/file.json');
final jsonResult = jsonDecode(data);
final parts = jsonResult[1];
return parts;
}
@override
Widget build(BuildContext context) {
return Scaffold(
body: FutureBuilder(
future: parts(), builder: (BuildContext context, AsyncSnapshot<dynamic> snapshot) {
if(snapshot.connectionState == ConnectionState.waiting){
return const CircularProgressIndicator(color: Colors.deepOrangeAccent);
}
return Container(
margin: const EdgeInsets.all(25.0),
child: ListView.builder(
itemCount: snapshot.data.length,
itemBuilder: (BuildContext context, int index) {
return Container(
margin: const EdgeInsets.only(bottom: 20.0),
child: ListTile(
title: Text(snapshot.data[index]['Name']),
),
);
},
),
);
},
),
);
}
}
如有任何帮助,我们将不胜感激!谢谢
可以使用where
条件进行筛选,
这是完整代码
class _ConstitutionPartsState extends State<ConstitutionParts> {
Future parts() async {
final data = await rootBundle.loadString('assets/json/file.json');
final jsonResult = jsonDecode(data);
return jsonResult; // return whole json
}
@override
Widget build(BuildContext context) {
return Scaffold(
body: FutureBuilder(
future: parts(), builder: (BuildContext context, AsyncSnapshot<dynamic> snapshot) {
if(snapshot.connectionState == ConnectionState.waiting){
return const CircularProgressIndicator(color: Colors.deepOrangeAccent);
}
return Container(
margin: const EdgeInsets.all(25.0),
child: ListView.builder(
itemCount: snapshot.data[1].length,
itemBuilder: (BuildContext context, int index) {
return Container(
margin: const EdgeInsets.only(bottom: 20.0),
child: ListTile(
title: Text(snapshot.data[index]['Name']),
onTap():{
//navigate to name page with
NewPage(articePartIndex : index, json: snapshot.data)
}
),
);
},
),
);
},
),
);
}
}
NewPage {
final List<List<dynamic>> json;
final int index;
@override
Widget build(BuildContext context) {
var articles = json[1][index]['Articles'];
var filteredArticleList = json[0].where((a) => articles.contains(a['ArtNo'])).toList();
return ListView.builder(
itemCount: snapshot.data.length,
itemBuilder: (BuildContext context, int index) {
return ListTile(
title: Text(filteredArticleList[index]['Name']),
),
});
}
}