简体   繁体   English

颤动下拉按钮

[英]Flutter DropDownButton

I try to display categories data in dropdownbutton , when I choose category I get this error,我尝试在dropdownbutton显示类别数据,当我选择category时出现此错误,

items == null || items.isEmpty || value == null ||

code DropDownButton :代码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);
  }


}

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如果您有任何问题,请给我 JSON 示例,也许我们可以解决它

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM