I try to display categories data in dropdownbutton
, when I choose category
I get this error,
items == null || items.isEmpty || value == null ||
code DropDownButton
:
class _DropDownButtonState extends State<DropDownButton> {
final CollectionReference categoryData = Firestore.instance
.collection('category')
.document('house')
.collection('subCategory');
Stream<List<Categories>> categories;
Categories _currentCategory;
@override
void initState() {
categories = categoriesData;
super.initState();
}
@override
Widget build(BuildContext context) {
return StreamBuilder<List<Categories>>(
stream: categories,
builder: ((context, snapshot) {
if (!snapshot.hasData) return CircularProgressIndicator();
return DropdownButton(
hint: Text('choose category'),
value: _currentCategory,
items: snapshot.data
.map((doc) => DropdownMenuItem(
child: Text(doc.categoryname),
value: doc,
))
.toList(),
onChanged: (doc) => setState(() {
_currentCategory = doc;
print(_currentCategory.categoryname);
}),
);
}));
}
Future createHouseCategory(_categorynameController) async {
var id = Uuid();
String categoryId = id.v1();
await categoryData
.document(categoryId)
.setData({'categoryname': _categorynameController});
}
List<Categories> _categoriesDataFromSnapshot(QuerySnapshot snapshot) {
return snapshot.documents.map((doc) {
return Categories(
categoryname: doc.data['categoryname'],
);
}).toList();
}
Stream<List<Categories>> get categoriesData {
return categoryData.snapshots().map(_categoriesDataFromSnapshot);
}
}