Dropdown in ListView

I have listview that contain this dropdown. Items of the drop are shown, it’s from API, but it throw some error when i select the value

Either zero or 2 or more [DropdownMenuItem]s were detected with the same value
‘package:flutter/src/material/dropdown.dart’:
Failed assertion: line 888 pos 15: ‘items == null || items.isEmpty || value == null ||
items.where((DropdownMenuItem item) {
return item.value == value;
}).length == 1′”

I guess it’s throw that error because of duplicate dropdown in the lisview that use same item.I think it’s should use index for different the dropdown. Any suggestions code examples please!

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>List? answerList;
</code>
<code>List? answerList; </code>
List? answerList;

This is function to fetch data:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>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');
}
}
</code>
<code>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'); } } </code>
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');
    } 
    
  }

This is dropdown that show the item:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code> 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(),
});
</code>
<code> 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(), }); </code>
 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(),
                  });

Ensure that the selectedValue is unique for each item

New contributor

Gaganjot Singh is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.

That means your answerList variable has 2 identical values. Because DropdownMenuItem value must be unique.

In example:

Bad

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>List answerList = [1,2,3,3];
</code>
<code>List answerList = [1,2,3,3]; </code>
List answerList = [1,2,3,3];

It’s bad because answer has two 3 (not unique), it will confusing DropdownButton

Good

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>List answerList = [1, 3, 5, 6, 8, 9];
</code>
<code>List answerList = [1, 3, 5, 6, 8, 9]; </code>
List answerList = [1, 3, 5, 6, 8, 9];

It’s good because all value in the list is unique. So DropdownButton can identify which one is selected by your value

Code Example

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>DropdownButton(
items: answerList.map(
(answer) => DropdownMenuItem(
// So when you iterate this dropdownmenuitem, make sure
// this [value] is unique, no duplicated [answer].
value: answer,
child: Text('$answer'),
),
),
onChanged: onChanged,
)
</code>
<code>DropdownButton( items: answerList.map( (answer) => DropdownMenuItem( // So when you iterate this dropdownmenuitem, make sure // this [value] is unique, no duplicated [answer]. value: answer, child: Text('$answer'), ), ), onChanged: onChanged, ) </code>
DropdownButton(
  items: answerList.map(
    (answer) => DropdownMenuItem(
      // So when you iterate this dropdownmenuitem, make sure
      // this [value] is unique, no duplicated [answer].
      value: answer,
      child: Text('$answer'),
    ),
  ),
  onChanged: onChanged,
)

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật