Reputation: 19
I've created a dropdown menu and when users click on certain items, it gives them the option to delete it. However, when I try to get the document ID of the selected item it instead returns the value of what I selected.
When I click on Email in my dropdown menu it prints "Email" which is a field value 'Name' : 'Email' from document DjgTDgbW4CkHMURnz32T. However, when I try and print the document ID of "Email", the field value just gets printed instead.
So far I know that userDocSTRING & userCatSTRING are correct and when I print _selectedCATEGORY, the correct value is also printed. But for some reason when I put .documentID at the end, "Email" gets printed instead. Flutter doctor returns no errors and not error logs get printed either.
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))
]))))
]);
}
}
Upvotes: 0
Views: 132
Reputation: 404
Based on your comments, try this:
var query = Firestore.instance
.collection('users')
.document(userDocSTRING.toString())
.collection(userCatSTRING.toString())
.where('Name', isEqualTo: _selectedCATEGORY);
var category = (await query.getDocuments()).documents[0].documentID;
But because of how complicated this is, I would consider using the categories' names as their IDs. So instead of a random ID, you'll have "Email" as the ID of the category "Email".
Upvotes: 1