firebase - Flutter - 提交表单时如何将DropDownMenu的选定索引保存到FireStore
问题描述
这是我的下拉列表代码
Container(
decoration: BoxDecoration(
color: Colors.lightBlueAccent,
//border: ,
borderRadius: BorderRadius.circular(20),
),
//color: Colors.white,
margin:
EdgeInsets.only(left: 50, right: 50),
child: StreamBuilder<QuerySnapshot>(
stream: Firestore.instance
.collection("Choose Platform")
.snapshots(),
builder: (context, snapshot) {
if (!snapshot.hasData)
const Text("Loading.....");
else {
List<DropdownMenuItem>
chooseplatform = [];
for (int i = 0;
i <
snapshot.data.documents
.length;
i++) {
DocumentSnapshot snap =
snapshot.data.documents[i];
chooseplatform.add(
DropdownMenuItem(
child: Text(
snap.documentID,
style: TextStyle(
color: Colors.black),
),
value: "${snap.documentID}",
),
);
}
return Row(
mainAxisAlignment:
MainAxisAlignment.center,
children: <Widget>[
DropdownButton(
items: chooseplatform,
onChanged:
(choosingplatform) {
final snackBar = SnackBar(
content: Text(
'Selected Platform is $choosingplatform',
style: TextStyle(
color: Color(
0xff1ffcb7)),
),
);
Scaffold.of(context)
.showSnackBar(
snackBar);
setState(() {
chosenPlatform =
choosingplatform;
});
},
value: chosenPlatform,
isExpanded: false,
hint: new Text(
"Choose Platform",
style: TextStyle(
color: Colors.black),
),
),
],
);
}
})),
这是 Raised 按钮的代码
RaisedButton(
padding:
EdgeInsets.only(left: 10, right: 10),
onPressed: () async {
Firestore.instance.runTransaction(
(Transaction transaction) async {
final CollectionReference reference =
Firestore.instance
.collection('Tournaments');
await reference
.document('Fifa Tournaments')
.collection('Fifa Tourneys')
.add({
'tourneyname':
tourneynameController.text,
'tourneygame':
tourneydateController.text,
});
tourneynameController.clear();
tourneydateController.clear();
});
Navigator.of(context).pop();
},
child: Text(
'Create Tournament',
style: TextStyle(fontSize: 16),
),
color: const Color(0xff1ffcb7),
textColor: Colors.white,
shape: RoundedRectangleBorder(
borderRadius:
BorderRadius.circular(18.0),
side: BorderSide(color: Colors.white),
),
),
现在,我只能保存 TextFormFields 的值,而不能保存下拉列表。简而言之,我现在正在做的是从集合中检索值并将它们显示在下拉菜单中,但我想要做的是检索值然后将选定的值保存在另一个名为“Fifa tourneys”的集合中已提交。谢谢!
解决方案
解决了!我所要做的就是将 selectedIndex 的变量保存到我的 firestore 集合中。
onPressed: () async {
Firestore.instance.runTransaction(
(Transaction transaction) async {
final CollectionReference reference =
Firestore.instance
.collection('Tournaments');
await reference
.document('Fifa Tournaments')
.collection('Fifa Tourneys')
.add({
'tourneyname':
tourneynameController.text,
'tourneygame':
tourneydateController.text,
'tourneyplatform': chosenPlatform,// this is the one
});
tourneynameController.clear();
tourneydateController.clear();
chosenPlatform.clear();
});
Navigator.of(context).pop();
},
推荐阅读
- activemq-artemis - JMS 主题订阅者在未连接时不会收到消息
- android - 如何在不使用任何构建工具的情况下使用 aapt2 编译 Android 应用程序?
- php - 是否可以在 PHP MYSQL 中存储可点击的图像?
- svg - svg 在浏览器和 imagemagick 中呈现不同(使用 rsvg 转换)
- css - Angular-ui-grid - 文本装饰不适用于行模板子项
- ios - UIImageWriteToSavedPhotosAlbum 错误:使用未解析的标识符
- python-2.7 - ubuntu playbook 中的 Python 非 ASCII 字符 '\xe2'
- php - 将 POST 回显到 SQL 字符串中
- sql - Eagerload has_one 与具有多个相关值的表的关联
- java - 如何使用流比较字符串字符?