如何在下拉中显示嵌套依赖的动态数据?
我有一个JSON数据,我想将其显示在下拉菜单中,以便最初的一个下拉列表将显示名称,然后选择它应该显示孩子下拉菜,每个孩子下拉菜也都有自己的孩子。
class LocationDetail {
final String name;
final String id;
final String categoryId;
final List<LocationDetail>? childrens;
const LocationDetail({
required this.name,
required this.id,
required this.categoryId,
this.childrens,
});
Map<String, dynamic> toMap() {
return {
'name': name,
'id': id,
'categoryId': categoryId,
'childrens': childrens,
};
}
factory LocationDetail.fromMap(Map<String, dynamic> map) {
List<LocationDetail> childrens = [];
if (map['childrens'] != null) {
map['childrens'].forEach((v) {
childrens.add(LocationDetail.fromMap(v));
});
}
return LocationDetail(
name: map['name'] as String,
id: map['id'] as String,
categoryId: map['categoryId'] as String,
childrens: childrens,
);
}
}
这是位置。json数据
[
{
"name": "NewYork",
"id": "NY",
"categoryId": "A",
"childrens": [
{
"name": "Rental Car 1",
"id": "NY-Rental Car1",
"categoryId": "T1"
},
{
"name": "Rental Car 2",
"id": "NY-Rental Car2",
"categoryId": "T2",
"childrens": [
{
"name": "Rental Car 21",
"id": "NY-Rental Car21",
"categoryId": "T21"
},
{
"name": "Rental Car 22",
"id": "NY-Rental Car22",
"categoryId": "T22"
}
]
}
]
}
]
这是我正在阅读JSON并转换为“位置尾巴
class HomePage extends StatefulWidget {
const HomePage({Key? key}) : super(key: key);
@override
State<HomePage> createState() => _HomePageState();
}
class _HomePageState extends State<HomePage> {
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(),
body: FutureBuilder<List<LocationDetail>>(
future: _getJsonData(),
builder: (context, snapshot) {
if (snapshot.hasData) {
// all the drop down will come here
} else if (snapshot.hasError) {
return Center(
child: Text(snapshot.error.toString()),
);
} else {
return const Center(
child: CircularProgressIndicator(),
);
}
},
),
);
}
Future<List<LocationDetail>> _getJsonData() async {
final contents = await rootBundle.loadString(
'assets/location.json',
);
List<dynamic> dataList = jsonDecode(contents);
List<LocationDetail> locationList =
dataList.map((v) => LocationDetail.fromMap(v)).toList();
return locationList;
}
}
预期输出”列表的方式:
- 最初只有一个下拉列表应显示仅包含 newyork 的下拉列表。
- 选择 nework 时,应该再显示一个下拉菜单,其中包含租车1 和租车2 。
- 如果选择租车1 ,则什么都不应发生,因为这是最后一个节点。
- 如果选择租车2 再下一个下拉订单应显示租车21 或租车22 。
- 在我们到达结束之前,也应该发生同样的事情。
PS谢谢
I have a json data, I want to display it in drop down such that initial one drop down will shown with name and upon selecting on that it should display child drop down and each child drop down has its own children as well.
class LocationDetail {
final String name;
final String id;
final String categoryId;
final List<LocationDetail>? childrens;
const LocationDetail({
required this.name,
required this.id,
required this.categoryId,
this.childrens,
});
Map<String, dynamic> toMap() {
return {
'name': name,
'id': id,
'categoryId': categoryId,
'childrens': childrens,
};
}
factory LocationDetail.fromMap(Map<String, dynamic> map) {
List<LocationDetail> childrens = [];
if (map['childrens'] != null) {
map['childrens'].forEach((v) {
childrens.add(LocationDetail.fromMap(v));
});
}
return LocationDetail(
name: map['name'] as String,
id: map['id'] as String,
categoryId: map['categoryId'] as String,
childrens: childrens,
);
}
}
Here is the location.json data
[
{
"name": "NewYork",
"id": "NY",
"categoryId": "A",
"childrens": [
{
"name": "Rental Car 1",
"id": "NY-Rental Car1",
"categoryId": "T1"
},
{
"name": "Rental Car 2",
"id": "NY-Rental Car2",
"categoryId": "T2",
"childrens": [
{
"name": "Rental Car 21",
"id": "NY-Rental Car21",
"categoryId": "T21"
},
{
"name": "Rental Car 22",
"id": "NY-Rental Car22",
"categoryId": "T22"
}
]
}
]
}
]
This is how I am reading json and converting to List of locationDetail
class HomePage extends StatefulWidget {
const HomePage({Key? key}) : super(key: key);
@override
State<HomePage> createState() => _HomePageState();
}
class _HomePageState extends State<HomePage> {
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(),
body: FutureBuilder<List<LocationDetail>>(
future: _getJsonData(),
builder: (context, snapshot) {
if (snapshot.hasData) {
// all the drop down will come here
} else if (snapshot.hasError) {
return Center(
child: Text(snapshot.error.toString()),
);
} else {
return const Center(
child: CircularProgressIndicator(),
);
}
},
),
);
}
Future<List<LocationDetail>> _getJsonData() async {
final contents = await rootBundle.loadString(
'assets/location.json',
);
List<dynamic> dataList = jsonDecode(contents);
List<LocationDetail> locationList =
dataList.map((v) => LocationDetail.fromMap(v)).toList();
return locationList;
}
}
Expected Output:
- Initially only one drop down should show which contain only NewYork.
- Upon selection on NewYork should show one more drop down which contains Rental Car 1 and Rental Car 2.
- If Rental Car 1 is selected nothing should happened since that is the last node.
- If Rental Car 2 is selected one more drop down should show to select Rental Car 21 or Rental Car 22.
- Same should happened till we reach to end.
P.S Thanks
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论