gpt4 book ai didi

flutter - Flutter:如何对数据使用forEach方法

转载 作者:行者123 更新时间:2023-12-03 04:36:54 27 4
gpt4 key购买 nike

我有2个JSON文件:
Json1文件:

[
{
"json1language": "English"
},
{
"json1language": "French"
},
{
"json1language": "Spanish"
}
]
Json2文件:
[
{
"json2country": "Canada",
"json2languages": [
{
"json2language": "English"
},
{
"json2language": "French"
}
]
},
{
"json2country": "Mexico",
"json2languages": [
{
"json2language": "English"
},
{
"json2language": "Spanish"
}
]
},
{
"json2country": "France",
"json2languages": [
{
"json2language": "French"
}
]
}
]
我希望当我单击 json1language => showDialog时显示 与条件json2country 匹配的 json2languagejson2language == json1language示例:单击“英语” => show json2country:“加拿大”; json2language:“英语”,“法语”和json2country:“Mexico”; json2language:“英语”,“西类牙语”( ,因为json2language包含“英语” )
enter image description here
所以请帮助我,这是主要文件
import 'package:flutter/material.dart';

import 'dart:convert';

List<Json1> json1FromJson(String str) => List<Json1>.from(json.decode(str).map((x) => Json1.fromJson(x)));

String json1ToJson(List<Json1> data) => json.encode(List<dynamic>.from(data.map((x) => x.toJson())));

class Json1 {
Json1({
this.json1Language,
});

String json1Language;

factory Json1.fromJson(Map<String, dynamic> json) => Json1(
json1Language: json["json1language"],
);

Map<String, dynamic> toJson() => {
"json1language": json1Language,
};
}

List<Json2> json2FromJson(String str) => List<Json2>.from(json.decode(str).map((x) => Json2.fromJson(x)));

String json2ToJson(List<Json2> data) => json.encode(List<dynamic>.from(data.map((x) => x.toJson())));

class Json2 {
Json2({
this.json2Country,
this.json2Languages,
});

String json2Country;
List<Json2Language> json2Languages;

factory Json2.fromJson(Map<String, dynamic> json) => Json2(
json2Country: json["json2country"],
json2Languages: List<Json2Language>.from(json["json2languages"].map((x) => Json2Language.fromJson(x))),
);

Map<String, dynamic> toJson() => {
"json2country": json2Country,
"json2languages": List<dynamic>.from(json2Languages.map((x) => x.toJson())),
};
}

class Json2Language {
Json2Language({
this.json2Language,
});

String json2Language;

factory Json2Language.fromJson(Map<String, dynamic> json) => Json2Language(
json2Language: json["json2language"],
);

Map<String, dynamic> toJson() => {
"json2language": json2Language,
};
}

class Json1Services {
static Future<List<Json1>> getData() {
String jsonString = '''
[
{
"json1language": "English"
},
{
"json1language": "French"
},
{
"json1language": "Spanish"
}
]
''';

return Future.value(json1FromJson(jsonString));
}
}

class Json2Services {
static Future<List<Json2>> getData() {
String jsonString = '''
[
{
"json2country": "Canada",
"json2languages": [
{
"json2language": "English"
},
{
"json2language": "French"
}
]
},
{
"json2country": "Mexico",
"json2languages": [
{
"json2language": "English"
},
{
"json2language": "Spanish"
}
]
},
{
"json2country": "France",
"json2languages": [
{
"json2language": "French"
}
]
}
]
''';

return Future.value(json2FromJson(jsonString));
}
}

class Json extends StatefulWidget {
@override
_JsonState createState() => _JsonState();
}

class _JsonState extends State<Json> {
List<Json1> _json1 = [];
List<Json2> _json2 = [];
@override
void initState() {
super.initState();
setState(() {
Json1Services.getData().then((data) {
setState(() {
_json1 = data;
});
});
Json2Services.getData().then((data) {
setState(() {
_json2 = data;
});
});
});
}

@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(),
body: SingleChildScrollView(
child: Row(
mainAxisAlignment: MainAxisAlignment.spaceEvenly,
children: [
for (Json1 j1 in _json1)
RaisedButton(
child: Text(j1.json1Language),
onPressed: () => showDialog(
context: context,
builder: (_) {
return AlertDialog(
content: null,
);
}))
],
),
));
}
}

最佳答案

您可以在下面复制粘贴运行完整代码
程式码片段

filtered = [];
_json2.forEach((element) {
for (int i = 0;
i < element.json2Languages.length;
i++) {
if (element.json2Languages[i].json2Language ==
j1.json1Language) {
filtered.add(element);
}
}
});

...

return AlertDialog(
content: SizedBox(
width: 300,
height: 70,
child: Row(
children: [
Expanded(
flex: 2,
child: Column(
mainAxisAlignment:
MainAxisAlignment.spaceAround,
crossAxisAlignment:
CrossAxisAlignment.center,
children: [
for (int i = 0;
i < filtered.length;
i++)
Row(
mainAxisAlignment:
MainAxisAlignment.spaceBetween,
children: [
Text(
filtered[i].json2Country,
),
for (int j = 0;
j <
filtered[i]
.json2Languages
.length;
j++)
Column(children: [
Text(filtered[i]
.json2Languages[j]
.json2Language)
]),
],
工作演示
enter image description here
完整的代码
import 'package:flutter/material.dart';
import 'package:flutter/material.dart';

import 'dart:convert';

List<Json1> json1FromJson(String str) =>
List<Json1>.from(json.decode(str).map((x) => Json1.fromJson(x)));

String json1ToJson(List<Json1> data) =>
json.encode(List<dynamic>.from(data.map((x) => x.toJson())));

class Json1 {
Json1({
this.json1Language,
});

String json1Language;

factory Json1.fromJson(Map<String, dynamic> json) => Json1(
json1Language: json["json1language"],
);

Map<String, dynamic> toJson() => {
"json1language": json1Language,
};
}

List<Json2> json2FromJson(String str) =>
List<Json2>.from(json.decode(str).map((x) => Json2.fromJson(x)));

String json2ToJson(List<Json2> data) =>
json.encode(List<dynamic>.from(data.map((x) => x.toJson())));

class Json2 {
Json2({
this.json2Country,
this.json2Languages,
});

String json2Country;
List<Json2Language> json2Languages;

factory Json2.fromJson(Map<String, dynamic> json) => Json2(
json2Country: json["json2country"],
json2Languages: List<Json2Language>.from(
json["json2languages"].map((x) => Json2Language.fromJson(x))),
);

Map<String, dynamic> toJson() => {
"json2country": json2Country,
"json2languages":
List<dynamic>.from(json2Languages.map((x) => x.toJson())),
};
}

class Json2Language {
Json2Language({
this.json2Language,
});

String json2Language;

factory Json2Language.fromJson(Map<String, dynamic> json) => Json2Language(
json2Language: json["json2language"],
);

Map<String, dynamic> toJson() => {
"json2language": json2Language,
};
}

class Json1Services {
static Future<List<Json1>> getData() {
String jsonString = '''
[
{
"json1language": "English"
},
{
"json1language": "French"
},
{
"json1language": "Spanish"
}
]
''';

return Future.value(json1FromJson(jsonString));
}
}

class Json2Services {
static Future<List<Json2>> getData() {
String jsonString = '''
[
{
"json2country": "Canada",
"json2languages": [
{
"json2language": "English"
},
{
"json2language": "French"
}
]
},
{
"json2country": "Mexico",
"json2languages": [
{
"json2language": "English"
},
{
"json2language": "Spanish"
}
]
},
{
"json2country": "France",
"json2languages": [
{
"json2language": "French"
}
]
}
]
''';

return Future.value(json2FromJson(jsonString));
}
}

class Json extends StatefulWidget {
@override
_JsonState createState() => _JsonState();
}

class _JsonState extends State<Json> {
List<Json1> _json1 = [];
List<Json2> _json2 = [];
@override
void initState() {
super.initState();
setState(() {
Json1Services.getData().then((data) {
setState(() {
_json1 = data;
});
});
Json2Services.getData().then((data) {
setState(() {
_json2 = data;
});
});
});
}

List<Json2> filtered = [];

@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(),
body: SingleChildScrollView(
child: Row(
mainAxisAlignment: MainAxisAlignment.spaceEvenly,
children: [
for (Json1 j1 in _json1)
RaisedButton(
child: Text(j1.json1Language),
onPressed: () => showDialog(
context: context,
builder: (_) {
filtered = [];
_json2.forEach((element) {
for (int i = 0;
i < element.json2Languages.length;
i++) {
if (element.json2Languages[i].json2Language ==
j1.json1Language) {
filtered.add(element);
}
}
});

return AlertDialog(
content: SizedBox(
width: 300,
height: 70,
child: Row(
children: [
Expanded(
flex: 2,
child: Column(
mainAxisAlignment:
MainAxisAlignment.spaceAround,
crossAxisAlignment:
CrossAxisAlignment.center,
children: [
for (int i = 0;
i < filtered.length;
i++)
Row(
mainAxisAlignment:
MainAxisAlignment.spaceBetween,
children: [
Text(
filtered[i].json2Country,
),
for (int j = 0;
j <
filtered[i]
.json2Languages
.length;
j++)
Column(children: [
Text(filtered[i]
.json2Languages[j]
.json2Language)
]),
],
)
],
),
)
],
),
),
);
}))
],
),
));
}
}

void main() {
runApp(MyApp());
}

class MyApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
return MaterialApp(
title: 'Flutter Demo',
theme: ThemeData(
primarySwatch: Colors.blue,
visualDensity: VisualDensity.adaptivePlatformDensity,
),
home: Json(),
);
}
}

class MyHomePage extends StatefulWidget {
MyHomePage({Key key, this.title}) : super(key: key);

final String title;

@override
_MyHomePageState createState() => _MyHomePageState();
}

class _MyHomePageState extends State<MyHomePage> {
int _counter = 0;

void _incrementCounter() {
setState(() {
_counter++;
});
}

@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text(widget.title),
),
body: Center(
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: <Widget>[
Text(
'You have pushed the button this many times:',
),
Text(
'$_counter',
style: Theme.of(context).textTheme.headline4,
),
],
),
),
floatingActionButton: FloatingActionButton(
onPressed: _incrementCounter,
tooltip: 'Increment',
child: Icon(Icons.add),
),
);
}
}

关于flutter - Flutter:如何对数据使用forEach方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/63948096/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com