-1

First of all, I try to google first but still not find the solution. Also, I try many options that I found on SO from googling but still not working. So, I open the question.

My code:

class AddRiderScreen extends StatefulWidget {
  @override
  _AddRiderScreenState createState() => _AddRiderScreenState();
}

class _AddRiderScreenState extends State<AddRiderScreen> {
  String _riderNameSelected = '';

  final List _listRiderName = [
    'Personal Accident (Risk A)',
    'Personal Accident (Risk B)',
    'Personal Accident (Risk C)'
  ];

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      backgroundColor: Colors.white,
      appBar: AppBar(
        title: Text('Title'),
        backgroundColor: Colors.white,
      ),
      body: Padding(
        padding: const EdgeInsets.all(16),
        child: DropdownButtonHideUnderline(
          child: DropdownButton(
            hint: Text('-- Pilih --'),
            isExpanded: true,
            value: _riderNameSelected,
            items: _listRiderName.map((value) {
              return DropdownMenuItem(
                child: Text(value),
                value: value,
              );
            }).toList(),
            icon: Icon(
              Icons.arrow_drop_down,
              color: Colors.grey,
            ),
            onChanged: (value) {
              setState(() {
                _riderNameSelected = value;
              });
            },
          ),
        ),
      ),
    );
  }
}

The Error:

There should be exactly one item with [DropdownButton]'s value: A. 
Either zero or 2 or more [DropdownMenuItem]s were detected with the same value
'package:flutter/src/material/dropdown.dart':
Failed assertion: line 834 pos 15: 'items == null || items.isEmpty || value == null ||
              items.where((DropdownMenuItem<T> item) {
                return item.value == value;
              }).length == 1'

Can someone know this problem and found the solution to how to fix this? Because I don't know how to fix this problem.

R Rifa Fauzi Komara
  • 1,915
  • 6
  • 27
  • 54
  • try `String _riderNameSelected = 'Personal Accident (Risk A)';` for example – pskink Feb 04 '21 at 10:50
  • Still not work, u can try it – R Rifa Fauzi Komara Feb 04 '21 at 10:55
  • 1
    Does this answer your question? [Flutter: There should be exactly one item with \[DropdownButton\]'s value](https://stackoverflow.com/questions/60510150/flutter-there-should-be-exactly-one-item-with-dropdownbuttons-value) – Balasubramani Sundaram Feb 04 '21 at 11:00
  • Please refer the solution here https://stackoverflow.com/questions/60510150/flutter-there-should-be-exactly-one-item-with-dropdownbuttons-value?rq=1 – Balasubramani Sundaram Feb 04 '21 at 11:00
  • @pskink No, because I already add `hint` on the `DropdownButton` widget. So when this class is rendered, the `DropdownButton` button will show `-- Pilih --` first, not the value from `_listRiderName` array. – R Rifa Fauzi Komara Feb 04 '21 at 11:03
  • @BalasubramaniSundaram I already try that solution, but still getting the same error. You can try it then you will know what I get from that error. – R Rifa Fauzi Komara Feb 04 '21 at 11:04
  • @pskink I found the problem, it's because I add `''` in the `_riderNameSelected` variable. So I just remove that and the default value `-- Pilih --` is shown – R Rifa Fauzi Komara Feb 04 '21 at 11:12
  • this is what i said in my first comment here, you need for example: `String _riderNameSelected = 'Personal Accident (Risk A)';` (or just `null`) - not `String _riderNameSelected = ';` – pskink Feb 04 '21 at 11:14

1 Answers1

0

Just remove '' in your _riderNameSelected.

String _riderNameSelected;
R Rifa Fauzi Komara
  • 1,915
  • 6
  • 27
  • 54