1

My api return number if data exists in database, or return null if don't exists.

child: DropdownButton<String>(
       hint: new Text("Select"),
       value: widget.car.type.toString(),

When data exists default value work fine, but when data is null get error:

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

Any solution how solve this problem, if value is null.

When put value: null, work but for all item must select item from list.

IS it possible here value: widget.car.type.toString(),

some like this is it possible:

value: widget.car.type.toString() ?? null,

babayaro
  • 491
  • 3
  • 15

2 Answers2

2

1st make sure you are getting data from api. Then we have make unique list of dropitems and we are going to use Set() here.

full demo:

import 'package:flutter/material.dart';

class StraggedExample extends StatefulWidget {
  const StraggedExample({Key? key}) : super(key: key);

  @override
  _StraggedExampleState createState() => _StraggedExampleState();
}

class _StraggedExampleState extends State<StraggedExample> {
  final fromAPi = ["a", "e", "f", "a"];

  late final dropitems;
  late String initValue;

  @override
  void initState() {
    super.initState();
    final values = fromAPi.toSet().toList();
    dropitems = List.generate(
      values.length,
      (index) => DropdownMenuItem(
        child: Text("item $index"),
        value: values[index],
      ),
    );

    initValue = values[0];
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
        body: Column(
      children: [
        DropdownButton(
          value: initValue,
          items: dropitems,
          onChanged: (value) {
            setState(() {
              initValue = value as String;
            });
          },
        ),
      ],
    ));
  }
}
Md. Yeasin Sheikh
  • 54,221
  • 7
  • 29
  • 56
  • Hello, My DropdownMenuItem load correctly on details.dart page, but when I come from selectAll.dart page and send value to details.dart get error if values is null. – babayaro Aug 05 '21 at 13:48
  • ok means, after pop()?, i think there is `state` problem. hope you are using `push` to go on `selectAll.dart`? – Md. Yeasin Sheikh Aug 05 '21 at 14:30
  • Tnx for help, I'm add if statment and now work fine. I don't know is that the best solution. – babayaro Aug 09 '21 at 07:57
0

For me below example work fine, I don't know is that good solution.

  String _myType;
  
  
  if (widget.car.type != null) {
     _myType= widget.car.type.toString();
  }
  else{
     _myType;
  }


  child: DropdownButton<String>(
  hint: new Text("Select"),
  value: _myType,  //here assign value
babayaro
  • 491
  • 3
  • 15