Exception has occurred. _AssertionError (‘package:flutter/src/material/dropdown.dart’:

huangapple go评论64阅读模式
英文:

Exception has occurred. _AssertionError ('package:flutter/src/material/dropdown.dart':

问题

你好,我从浏览器中复制了代码,但在尝试时出现了错误,我该如何解决,因为我想将此代码用作学习参考,但出现了以下错误。

异常情况发生。

_AssertionError('package:flutter/src/material/dropdown.dart':断言失败:第1584行之后:'items == null || items.isEmpty || value == null ||
               items.where((DropdownMenuItem<T> item) {
                 return item. value == value;
               }).length == 1':[DropdownButton]的值应该恰好有一个项目:Dropdown 1。
检测到具有相同值的[DropdownMenuItem]为零或2个或更多)
    void main() {
      runApp(const MyApp());
    }
    
    class MyApp extends StatelessWidget {
      const MyApp({super.key});
    
      @override
      Widget build(BuildContext context) {
        return MaterialApp(
          title: 'Flutter Demo',
          theme: ThemeData(
            primarySwatch: Colors.blue,
          ),
          home: DynamicDropdown(),
        );
      }
    }
    
    class DynamicDropdown extends StatefulWidget {
      @override
      _DynamicDropdownState createState() => _DynamicDropdownState();
    }
    
    class _DynamicDropdownState extends State<DynamicDropdown> {
      String dropdownValue1 = 'Dropdown 1';
      String dropdownValue2 = 'Dropdown 2';
      List<String> dropdownItems1 = ['Item 1', 'Item 2', 'Item 3'];
      List<String> dropdownItems2 = ['Item A', 'Item B', 'Item C'];
    
      @override
      Widget build(BuildContext context) {
        return Scaffold(
          appBar: AppBar(
            title: Text('Dynamic Dropdown'),
          ),
          body: Center(
            child: Column(
              mainAxisAlignment: MainAxisAlignment.center,
              children: <Widget>[
                DropdownButtonFormField(
                  value: dropdownValue1,
                  onChanged: (newValue) {
                    setState(() {
                      dropdownValue1 = newValue!;
                      if (dropdownValue1 == 'Item 1') {
                        dropdownItems2 = ['Item A', 'Item B', 'Item C'];
                      } else if (dropdownValue1 == 'Item 2') {
                        dropdownItems2 = ['Item D', 'Item E', 'Item F'];
                      } else {
                        dropdownItems2 = ['Item G', 'Item H', 'Item I'];
                      }
                    });
                  },
                  items: dropdownItems1.map((String value) {
                    return DropdownMenuItem<String>(
                      value: value,
                      child: Text(value),
                    );
                  }).toList(),
                ),
                SizedBox(height: 20),
                DropdownButton<String>(
                  value: dropdownValue2,
                  onChanged: (newValue) {
                    setState(() {
                      dropdownValue2 = newValue!;
                    });
                  },
                  items: dropdownItems2.map((String value) {
                    return DropdownMenuItem<String>(
                      value: value,
                      child: Text(value),
                    );
                  }).toList(),
                ),
              ],
            ),
          ),
        );
      }
    }
英文:

Hello I copied the code from the browser when I tried it an error occurred, how do I solve it because I want to use this code as a reference in learning but the following error occurs

Exception has occurred.

_AssertionError (&#39;package:flutter/src/material/dropdown.dart&#39;: Failed assertion: line 1584 post 15: &#39;items == null || items.isEmpty || value == null ||
items.where((DropdownMenuItem&lt;T&gt; item) {
return item. value == value;
}).length == 1&#39;: There should be exactly one item with [DropdownButton]&#39;s value: Dropdown 1.
Either zero or 2 or more [DropdownMenuItem]s were detected with the same value)
    void main() {
runApp(const MyApp());
}
class MyApp extends StatelessWidget {
const MyApp({super.key});
@override
Widget build(BuildContext context) {
return MaterialApp(
title: &#39;Flutter Demo&#39;,
theme: ThemeData(
primarySwatch: Colors.blue,
),
home: DynamicDropdown(),
);
}
}
class DynamicDropdown extends StatefulWidget {
@override
_DynamicDropdownState createState() =&gt; _DynamicDropdownState();
}
class _DynamicDropdownState extends State&lt;DynamicDropdown&gt; {
String dropdownValue1 = &#39;Dropdown 1&#39;;
String dropdownValue2 = &#39;Dropdown 2&#39;;
List&lt;String&gt; dropdownItems1 = [&#39;Item 1&#39;, &#39;Item 2&#39;, &#39;Item 3&#39;];
List&lt;String&gt; dropdownItems2 = [&#39;Item A&#39;, &#39;Item B&#39;, &#39;Item C&#39;];
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text(&#39;Dynamic Dropdown&#39;),
),
body: Center(
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: &lt;Widget&gt;[
DropdownButtonFormField(
value: dropdownValue1,
onChanged: (newValue) {
setState(() {
dropdownValue1 = newValue!;
if (dropdownValue1 == &#39;Item 1&#39;) {
dropdownItems2 = [&#39;Item A&#39;, &#39;Item B&#39;, &#39;Item C&#39;];
} else if (dropdownValue1 == &#39;Item 2&#39;) {
dropdownItems2 = [&#39;Item D&#39;, &#39;Item E&#39;, &#39;Item F&#39;];
} else {
dropdownItems2 = [&#39;Item G&#39;, &#39;Item H&#39;, &#39;Item I&#39;];
}
});
},
items: dropdownItems1.map((String value) {
return DropdownMenuItem&lt;String&gt;(
value: value,
child: Text(value),
);
}).toList(),
),
SizedBox(height: 20),
DropdownButton&lt;String&gt;(
value: dropdownValue2,
onChanged: (newValue) {
setState(() {
dropdownValue2 = newValue!;
});
},
items: dropdownItems2.map((String value) {
return DropdownMenuItem&lt;String&gt;(
value: value,
child: Text(value),
);
}).toList(),
),
],
),
),
);
}
}

答案1

得分: 1

your 'dropdownValue1' should be from 'dropdownItems1' list. error comes from here - 'value: dropdownValue1', where you have assigned item from outside the list, check below code, I have edited your code;

// String dropdownValue1 = 'Dropdown 1';
// String dropdownValue2 = 'Dropdown 2';
List dropdownItems1 = ['Item 1', 'Item 2', 'Item 3'];
List dropdownItems2 = ['Item A', 'Item B', 'Item C'];
String dropdownValue1;
String dropdownValue2;

@override
void initState() {
// TODO: implement initState
super.initState();
dropdownValue1 = dropdownItems1[0];
dropdownValue2 = dropdownItems2[0];
}

英文:

your 'dropdownValue1' should be from 'dropdownItems1' list. error comes from here - 'value: dropdownValue1' ,where you have assigned item from outside the list, check below code, I have edited your code;

  // String dropdownValue1 = &#39;Dropdown 1&#39;;
//  String dropdownValue2 = &#39;Dropdown 2&#39;;
List&lt;String&gt; dropdownItems1 = [&#39;Item 1&#39;, &#39;Item 2&#39;, &#39;Item 3&#39;];
List&lt;String&gt; dropdownItems2 = [&#39;Item A&#39;, &#39;Item B&#39;, &#39;Item C&#39;];
String dropdownValue1;
String dropdownValue2;
@override
void initState() {
// TODO: implement initState
super.initState();
dropdownValue1 = dropdownItems1[0];
dropdownValue2 = dropdownItems2[0];
}

huangapple
  • 本文由 发表于 2023年4月4日 10:16:37
  • 转载请务必保留本文链接:https://go.coder-hub.com/75925027.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定