【发布时间】:2021-10-28 20:04:08
【问题描述】:
我正在尝试制作一个包含名称列表的下拉按钮。 下拉列表应显示团队的运动员姓名。 但是,下拉列表仅显示 UID。我无法让它通过 UID 查询帐户以提取正确的名称。
代码:
FutureBuilder(
future: database.reference().child("teams").child("TID1").child("athletes").once(),
builder: (context, AsyncSnapshot<DataSnapshot> snapshot) {
if(snapshot.hasData) {
aKeys.clear();
Map<dynamic, dynamic> values = snapshot.data.value;
values.forEach((key, value) {
database.reference().child("accounts").child(value).once();
if (snapshot.hasData) {
Map<dynamic, dynamic> nameValues = snapshot.data.value;
nameValues.forEach((key, value) {
aNames.add(
DropdownMenuItem<dynamic>(
child: Text(value["first name"]),
value: key,
)
);
});
}
});
return Material(
color: Colors.white,
child: DropdownButtonHideUnderline(
child: DropdownButton(
items: aNames,
hint: Text("Select an athlete"),
value: _athlete,
onChanged: (value) {
setState(() {
_athlete = value;
});
},
),
),
);
}
return CircularProgressIndicator();
}
),
我知道问题出在 values.forEach 中,因为我正在尝试对帐户运行查询以提取名称。我知道这不起作用,因为它不是正确的方法,但我不知道让它工作的正确方法。我尝试在 values.forEach 中添加另一个 FutureBuilder,但这也不起作用。
我想通过查询来找出球队的运动员键是什么,然后将它们放入一个列表中。然后遍历该列表以对帐户运行查询,然后获取名字值并将其添加到具有名字的 DropdownMenuItem 的单独列表中。
JSON 数据:
{
"accounts" :{
"UID1" : {
"first name" : "bob",
"last name" : "rogers"
},
"UID2" : {
"first name" : "john",
"last name" : "smith"
},
"UID3" : {
"first name" : "tim",
"last name" : "murr"
},
"UID4" : {
"first name" : "larry",
"last name" : "dean"
}
},
"teams" : {
"TID1" : {
"athletes" : {
"A1" : "UID1",
"A2" : "UID2"
}
},
"TID2" : {
"athletes" : {
"A1" : "UID3",
"A2" : "UID4"
}
}
}
}
当前代码:
FutureBuilder(
future: database.reference().child("teams").child("UID").child("athletes").once(),
builder: (context, AsyncSnapshot<DataSnapshot> snapshot) {
if(snapshot.hasData) {
dynamic values = snapshot.data.value;
return FutureBuilder(
future: Future.wait<DataSnapshot>(values.map((key, value) {
return database.reference().child("accounts").child(value).once();
})),
builder: (context, AsyncSnapshot<List<DataSnapshot>> snapshots) {
snapshots.data!.forEach((snapshot) {
aNames.add(
DropdownMenuItem<dynamic>(
child: Text(snapshot.value["first name"]),
value: snapshot.key,
)
);
});
return Material(
color: Colors.white,
child: DropdownButtonHideUnderline(
child: DropdownButton(
items: aNames,
hint: Text("Select an athlete"),
value: _athlete,
onChanged: (value) {
setState(() {
_athlete = value;
});
},
),
),
);
},
);
}
return CircularProgressIndicator();
}
),
【问题讨论】:
标签: android flutter firebase-realtime-database