我创建了一个下拉菜单,当用户单击某些项目时,它为他们提供了将其删除的选项。但是,当我尝试获取所选项目的文档ID时,它将返回我所选内容的值。
当我在下拉菜单中单击“电子邮件”时,它会打印“ Email”,它是来自文档DjgTDgbW4CkHMURnz32T的字段值“名称”:“电子邮件”。但是,当我尝试打印“电子邮件”的文档ID时,只会打印字段值。
到目前为止,我知道userDocSTRING和userCatSTRING是正确的,并且当我打印_selectedCATEGORY时,也会打印正确的值。但是由于某种原因,当我将.documentID放在末尾时,将改为打印“ Email”。 Flutter Doctor不返回任何错误,也不会打印错误日志。
import 'package:cloud_firestore/cloud_firestore.dart';
import 'package:firepass/Assets/Colors/colors.dart';
import 'package:flutter/material.dart';
import 'package:firepass/Firebase/login_auth.dart';
// Instantiate stream for listening to category changes
// Used in all dropdown menus
class CategorySettings extends StatefulWidget {
CategorySettingsSTATE createState() => CategorySettingsSTATE();
}
class CategorySettingsSTATE extends State<CategorySettings> {
var selectedCATEGORY;
bool _deleteABLE = false;
@override
Widget build(BuildContext context) {
return StreamBuilder<QuerySnapshot>(
stream: Firestore.instance
.collection('users')
.document(userDocSTRING.toString())
.collection(userCatSTRING.toString())
.snapshots(),
builder: (context, snapshot) {
if (!snapshot.hasData) {
return Center(child: CircularProgressIndicator());
} else {
List<DropdownMenuItem> catItems = [];
for (int i = 0; i < snapshot.data.documents.length; i++) {
DocumentSnapshot snap = snapshot.data.documents[i];
catItems.add(DropdownMenuItem(
child: Text(snap.data['Name']),
value: "${snap.data['Name']}",
));
}
return Column(children: <Widget>[
DropdownButton(
items: catItems.toList(),
onChanged: (categoryVALUE) {
setState(() {
selectedCATEGORY = categoryVALUE;
DeleteButtonSTATE().delete = categoryVALUE;
if (categoryVALUE == 'All') {
_deleteABLE = false;
print(
'Category can be deleted ==== ${_deleteABLE.toString()}');
} else
_deleteABLE = true;
print(categoryVALUE.toString());
});
},
value: selectedCATEGORY,
isExpanded: false),
_deleteABLE == true
? DeleteBUTTON()
: Container(height: 0.0, width: 0.0)
]);
}
});
}
}
class DeleteBUTTON extends StatefulWidget {
DeleteButtonSTATE createState() => DeleteButtonSTATE();
}
class DeleteButtonSTATE extends State<DeleteBUTTON> {
var delete;
Widget build(BuildContext context) {
return Column(children: <Widget>[
Padding(padding: EdgeInsets.all(12)),
IconButton(
icon: Icon(Icons.lightbulb_outline),
onPressed: () {
print(delete.toString());
}),
GestureDetector(
onTap: () {
print('Category ${CategorySettingsSTATE().toString()} Deleted');
Firestore.instance
.collection('users')
.document(userDocSTRING.toString())
.collection(userCatSTRING.toString())
.document(CategorySettingsSTATE().selectedCATEGORY.toString())
.delete();
},
child: Card(
shape: RoundedRectangleBorder(
borderRadius: BorderRadius.circular(20)),
margin: EdgeInsets.symmetric(horizontal: 80),
elevation: 0,
color: accentRedColor,
child: Padding(
padding: EdgeInsets.all(10),
child: Row(
mainAxisAlignment: MainAxisAlignment.center,
children: <Widget>[
Icon(
Icons.delete_forever,
color: whiteCOLOR,
),
Padding(padding: EdgeInsets.symmetric(horizontal: 10)),
Text('Delete',
style:
TextStyle(color: whiteTextCOLOR, fontSize: 12))
]))))
]);
}
}
答案 0 :(得分:1)
根据您的评论,尝试以下操作:
var query = Firestore.instance
.collection('users')
.document(userDocSTRING.toString())
.collection(userCatSTRING.toString())
.where('Name', isEqualTo: _selectedCATEGORY);
var category = (await query.getDocuments()).documents[0].documentID;
但是由于这很复杂,所以我考虑使用类别名称作为其ID。因此,您将使用“电子邮件”作为类别“电子邮件”的ID,而不是随机ID。