ListView 中的下拉菜单

问题描述 投票:0回答:1

我有包含此下拉列表的列表视图。显示了掉落的物品,它来自 API,但当我选择值时它会抛出一些错误

检测到零个或 2 个或更多 [DropdownMenuItem] 具有相同的值 '包:flutter/src/material/dropdown.dart': 断言失败:第 888 行 pos 15:'items == null || items.isEmpty ||值==空|| items.where((DropdownMenuItem 项目) { 返回 item.value == value; }).长度== 1'"

我猜它会抛出该错误,因为 lisview 中使用相同项目的重复下拉列表。我认为应该对不同的下拉列表使用索引。请提供任何建议代码示例!

List? answerList;

这是获取数据的函数:

void answerSetupDropdown() async {
    var headers = {
      'Accept': 'application/json',
      'Content-Type': 'application/json',
    };
    var request =
        http.Request('POST', Uri.parse(baseLaravelAPI + '/api/start_section'));
    request.body = json.encode({
      "staffid": widget.staffId.toString(),
      "form_open_id": widget.formOpenId.toString(),
      "section_id": widget.sectionId.toString(),
      "form_id": widget.formId.toString(),
      "confirm_no": widget.confNo.toString()
    });
    request.headers.addAll(headers);

    http.StreamedResponse response = await request.send();

    if (response.statusCode == 200) {
      final res = jsonDecode(await response.stream.bytesToString());
      answerList = res["data"]["answer_list"];
      loading = false;
    } else {
      throw Exception('Unable to fetch products from the REST API');
    } 
    
  }

这是显示该项目的下拉菜单:

 ListView.builder(
                itemCount: chilList!.length,
                
                itemBuilder: (context, i) {
loading == false
                                    ? Expanded(
                                        // alignment: Alignment.centerLeft,
                                        child: Theme(
                                          data: Theme.of(context).copyWith(
                                            unselectedWidgetColor: lightblue,
                                          ),
                                          child: DropdownButton<dynamic>(
                                            underline: SizedBox(),
                                            hint: Text("Select Answer"),
                                            value: selectedValue,
                                            onChanged: (dynamic value) {
                                              setState(() {
                                                selectedValue =
                                                    (value["answer_name"])
                                                        .toString();
                                              });
                                              // selectedValue =
                                              //     (valueSel["answer_name"])
                                              //         .toString();
                                            },
                                            items: answerList!.map((answer) {
                                              return DropdownMenuItem<dynamic>(
                                                value: answer,
                                                child: Row(
                                                  children: <Widget>[
                                                    SizedBox(
                                                      width: 10,
                                                    ),
                                                    Text(
                                                      answer["answer_name"]
                                                          .toString(),
                                                      style: TextStyle(
                                                          color: Color(
                                                              int.parse((answer[
                                                                      "answer_color"])
                                                                  .replaceAll(
                                                                      RegExp(
                                                                          '#'),
                                                                      '0xFF')))),
                                                    ),
                                                  ],
                                                ),
                                              );
                                            }).toList(),
                                          ),
                                        ),
                                      )
                                    : CircularProgressIndicator(),
                  });
flutter
1个回答
0
投票

确保每件商品的

selectedValue
都是唯一的

© www.soinside.com 2019 - 2024. All rights reserved.