Home > Mobile >  Failed assertion:'items == null || items.isEmpty || value == null || items.where((DropdownMenuI
Failed assertion:'items == null || items.isEmpty || value == null || items.where((DropdownMenuI

Time:09-30

class DropDown extends StatefulWidget {
  const DropDown({
    this.data,
    this.hint,
    Key key,
  }) : super(key: key);
  final List<String> data;
  final String hint;
  @override
  _DropDownState createState() => _DropDownState();
}

String _chosenValue1;

class _DropDownState extends State<DropDown> {
  @override
  Widget build(BuildContext context) {
    return SingleChildScrollView(
      child: Container(
        width: 250,
        padding: const EdgeInsets.all(0.0),
        child: DropdownButton<String>(
          iconSize: 30,
          isExpanded: true,
          value: _chosenValue1,

          //elevation: 5,
          items: widget.data.map<DropdownMenuItem<String>>((String value) {
            return DropdownMenuItem<String>(
              value: value,
              child: Text(value),
            );
          }).toList(),
          hint: Text(
            widget.hint,
            style: TextStyle(
              color: Colors.black,
              fontSize: 13,
              fontWeight: FontWeight.w600,
            ),
          ),
          onChanged: (String value) {
            setState(() {
              _chosenValue1 = value;
            });
          },
        ),
      ),
    );
  }
}
   DropDown(
                        data: [
                          'Non-Blanchable',
                          'Partial thickness skin',
                          'Full thickness skin loss involving damage or necrosis',
                          'Obscured by necrosis'
                        ],
                        hint: 'Assessment',
                      ),
  DropDown(
                        data: [
                          'Indistinct, diffuse,none ',
                          'Distinct,outline clearly'
                        ],
                        hint: 'Assessment',
                      ),

i have been stuck on this problem for a while now, When i have the same data inside the data it works however all the dropdown would become the same, I want to be able to have different data for different dropdown , but when i do so the error is caused and i cant figure out whats wrong with it

CodePudding user response:

import 'package:flutter/material.dart';

class DropDown extends StatefulWidget {
  DropDown({
    this.data,
    this.hint,
    this.initialValue,
    Key? key,
  }) : super(key: key);
  final List<String>? data;
  final String? hint;
  final String? initialValue;
  String chosenValue1 = "";

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

class _DropDownState extends State<DropDown> {
  @override
  Widget build(BuildContext context) {
    return SingleChildScrollView(
      child: Container(
        width: 250,
        padding: const EdgeInsets.all(0.0),
        child: DropdownButton<String>(
          iconSize: 30,
          isExpanded: true,
          value: widget.initialValue!.isEmpty ? null : widget.initialValue!,

          //elevation: 5,
          items: widget.data!.map<DropdownMenuItem<String>>((String value) {
            return DropdownMenuItem<String>(
              value: value,
              child: Text(value),
            );
          }).toList(),
          hint: Text(
            widget.hint!,
            style: const TextStyle(
              color: Colors.black,
              fontSize: 13,
              fontWeight: FontWeight.w600,
            ),
          ),
          onChanged: (value) {
            setState(() {
              widget.chosenValue1 = value!;
            });
          },
        ),
      ),
    );
  }
}

import 'package:flutter/material.dart';

import 'dropdown.dart';

void main() {
  runApp(const MyApp());
}

class MyApp extends StatelessWidget {
  const MyApp({Key? key}) : super(key: key);

  // This widget is the root of your application.
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      title: 'Flutter Demo',
      theme: ThemeData(
        primarySwatch: Colors.blue,
      ),
      home: const MyHomePage(title: 'Flutter Demo Home Page'),
    );
  }
}

class MyHomePage extends StatefulWidget {
  const MyHomePage({Key? key, required this.title}) : super(key: key);
  final String title;

  @override
  State<MyHomePage> createState() => _MyHomePageState();
}

class _MyHomePageState extends State<MyHomePage> {
  int _counter = 0;

  void _incrementCounter() {
    setState(() {
      // This call to setState tells the Flutter framework that something has
      // changed in this State, which causes it to rerun the build method below
      // so that the display can reflect the updated values. If we changed
      // _counter without calling setState(), then the build method would not be
      // called again, and so nothing would appear to happen.
      _counter  ;
    });
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text(widget.title),
      ),
      body: Center(
        child: Column(
          mainAxisAlignment: MainAxisAlignment.center,
          children: <Widget>[
            DropDown(
              data: const [
                'Non-Blanchable',
                'Partial thickness skin',
                'Full thickness skin loss involving damage or necrosis',
                'Obscured by necrosis'
              ],
              hint: 'Assessment',
              initialValue: "Non-Blanchable",
            ),
            DropDown(
              data: const [
                'Indistinct, diffuse,none',
                'Distinct,outline clearly'
              ],
              hint: 'Assessment',
              initialValue: "",
            ),
          ],
        ),
      ),
      floatingActionButton: FloatingActionButton(
        onPressed: _incrementCounter,
        tooltip: 'Increment',
        child: const Icon(Icons.add),
      ), // This trailing comma makes auto-formatting nicer for build methods.
    );
  }
}

Use the above code it will fix ur error

CodePudding user response:

I tried running your code and, after making your data and hint required params and moving the chosenValue variable inside your _DropDownState, it works perfectly fine. Can you maybe share some steps with how to reproduce the error that you're seeing, because I see two different dropdowns with values I can select independently of each other.

As per your description of how to reproduce the error, I've tried adding navigation between two screens, but it still all works as intended.

import 'package:flutter/material.dart';

void main() => runApp(MyApp());

class MyApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      home: Dropdowns(),
    );
  }
}

class Dropdowns extends StatelessWidget {
  const Dropdowns();

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      body: Padding(
        padding: EdgeInsets.all(40),
        child: Column(
          children: [
            Text('This is the first screen'),
            DropDown(
              data: [
                'Non-Blanchable',
                'Partial thickness skin',
                'Full thickness skin loss involving damage or necrosis',
                'Obscured by necrosis'
              ],
              hint: 'Assessment',
            ),
            DropDown(
              data: ['Indistinct, diffuse,none ', 'Distinct,outline clearly'],
              hint: 'Assessment',
            ),
            ElevatedButton(
              child: Text('Go to second screen'),
              onPressed: () {
                Navigator.of(context).push(
                  MaterialPageRoute(
                    builder: (context) => SecondScreen(),
                  ),
                );
              },
            ),
          ],
        ),
      ),
    );
  }
}

class DropDown extends StatefulWidget {
  const DropDown({
    required this.data,
    required this.hint,
    Key? key,
  }) : super(key: key);
  final List<String> data;
  final String hint;
  @override
  _DropDownState createState() => _DropDownState();
}

class _DropDownState extends State<DropDown> {
  String? _chosenValue1;

  @override
  Widget build(BuildContext context) {
    return SingleChildScrollView(
      child: Container(
        width: 250,
        padding: const EdgeInsets.all(0.0),
        child: DropdownButton<String>(
          iconSize: 30,
          isExpanded: true,
          value: _chosenValue1,

          //elevation: 5,
          items: widget.data.map<DropdownMenuItem<String>>((String value) {
            return DropdownMenuItem<String>(
              value: value,
              child: Text(value),
            );
          }).toList(),
          hint: Text(
            widget.hint,
            style: TextStyle(
              color: Colors.black,
              fontSize: 13,
              fontWeight: FontWeight.w600,
            ),
          ),
          onChanged: (String? value) {
            setState(() {
              _chosenValue1 = value;
            });
          },
        ),
      ),
    );
  }
}

class SecondScreen extends StatelessWidget {
  const SecondScreen({Key? key}) : super(key: key);

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text('SECOND SCREEN'),
      ),
      body: Padding(
        padding: EdgeInsets.all(40),
        child: Column(
          children: [
            Text('This is the second screen'),
            DropDown(
              data: [
                'Non-Blanchable',
                'Partial thickness skin',
                'Full thickness skin loss involving damage or necrosis',
                'Obscured by necrosis'
              ],
              hint: 'Assessment',
            ),
            DropDown(
              data: ['Indistinct, diffuse,none ', 'Distinct,outline clearly'],
              hint: 'Assessment',
            ),
          ],
        ),
      ),
    );
  }
}
  • Related