Flutter DropDownButton

时间:2020-02-24 09:05:14

标签: flutter

我尝试在dropdownbutton中显示类别数据,当我选择category时出现此错误,

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

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


}

1 个答案:

答案 0 :(得分:0)

只需查看此链接即可解决下拉菜单问题:

Dropdown Button wont change

也许您没有为字段Field提供所需的参数,这就是为什么它没有使索引显示在下拉按钮中的原因。让我知道它是否有效。

如果您有任何问题,请给我JSON示例,也许我们可以进行处理