首页 > 解决方案 > 下拉菜单不选择数据颤动 Firestore

问题描述

有人可以解释一下为什么我不能在我的下拉列表中选择另一个项目。当我点击时,下拉按钮重置。我没有错误,所以我不知道问题出在哪里。

我遵循了颤振文档,但总是同样的错误。

  Widget dropdown(BuildContext context) {
    CollectionReference sneakers =
        FirebaseFirestore.instance.collection("sneakers");

    final _raffle = TextEditingController();

    String dropdownValue;
    return Column(
      children: [
        TextFormField(
          controller: _raffle,
          validator: (value) {
            if (value.isEmpty) {
              return 'Please enter some text';
            }
            return null;
          },
          decoration: InputDecoration(
            labelText: "raffles",
          ),
        ),
        StreamBuilder<QuerySnapshot>(
          stream: sneakers.snapshots(),
          builder:
              (BuildContext context, AsyncSnapshot<QuerySnapshot> snapshot) {
            if (snapshot.hasError) {
              return Text('Something went wrong');
            }

            if (snapshot.connectionState == ConnectionState.waiting) {
              return Text("Loading");
            }

            return new DropdownButton<String>(
              value: dropdownValue,
              icon: const Icon(Icons.arrow_downward),
              iconSize: 24,
              elevation: 16,
              style: const TextStyle(color: Colors.deepPurple),
              underline: Container(
                height: 2,
                color: Colors.deepPurpleAccent,
              ),
              onChanged: (String newValue) {
                setState(() {
                  dropdownValue = newValue;
                  print(newValue);
                });
              },
              items: snapshot.data.docs.map((DocumentSnapshot doc) {
                return new DropdownMenuItem<String>(
                    value: doc.reference.id.toString(),
                    child: Text(
                      doc['name'],
                    ));
              }).toList(),
            );
          },
        ),        

标签: firebaseflutterdartgoogle-cloud-firestoredropdown

解决方案


推荐阅读