0

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);
  }


}
Andrey Khan
  • 183
  • 1
  • 3
  • 13
  • where is the error ? – Ahmed Khattab Feb 24 '20 at 09:36
  • What do you mean? when i try to select category, after i get error Assertion failed: org-dartlang-app:///packages/flutter/src/material/dropdown.dart:803:15 items == null || items.isEmpty || value == null || items.where((DropdownMenuItem item) { return item.value == value; }).length == 1 "There should be exactly one item with [DropdownButton]'s value: Instance of 'Categories'. \nEither zero or 2 or more [DropdownMenuItem]s were detected with the same value" – Andrey Khan Feb 24 '20 at 09:41

1 Answers1

0

Just check out this link where I have solved the question for the dropdown :

Dropdown Button wont change

maybe you are not giving the desired parameters to the value Field, that's why it is not getting the index to show in the dropdown button. let me know if it works.

If you have any issue just give me the JSON sample, maybe we can work on it

Sagar Acharya
  • 3,397
  • 4
  • 12
  • 34
  • I added void initState and solved it. See my post i edited. Can you tell me this is a good way? – Andrey Khan Feb 24 '20 at 11:38
  • Actually I am not sure but according to this link: https://inducesmile.com/google-flutter/how-to-use-streambuilder-to-populate-dropdownbutton-in-flutter/, the way you have written is a good way. – Sagar Acharya Feb 24 '20 at 12:07