【问题标题】:Flutter : Dropdown Issues颤振:下拉问题
【发布时间】:2021-07-22 13:43:11
【问题描述】:

一旦我从下拉列表中选择一个值,就会出现此错误

'package: flutter/src/material/dropdown.dart/: 断言失败: line 855 pos 15: 'item == bull || item.isEmpty ||值 == 空值|| item.where((DropdownMenuItemitem){ retuen item.value == value;)}.length ==1':应该正好有一项具有 [DropdownButton] 的值:类别 1。零个或 2 个或多个 [DropdownMenuItem ]s 被检测到相同的值

我该如何纠正它?

这是我的代码。这将是一个很大的帮助。

import 'package:flutter/material.dart';
import 'package:flutter/cupertino.dart';

void main() {
runApp(MaterialApp(home: MyApp()));
}

class MyApp extends StatefulWidget {
@override
_MyAppState createState() => _MyAppState();
}

class _MyAppState extends State<MyApp> {
String valueChoose;
List _listItem = ["Category 1", "Category 2", "Category 3", "Category 4"];
List _listItem1 = [
"Sub Category 1",
"Sub Category 2",
"Sub Category 3",
"Sub Category 4"
];
List _listItem2 = ["CRIS", "ADMINISTRATION", "ZONE", "DEPARTMENT"];

var selectedState;
final GlobalKey<FormState> _formKey = GlobalKey<FormState>();

@override
Widget build(BuildContext context) {
return Scaffold(
    appBar: AppBar(
      title: Text("Feedback"),
      centerTitle: true,
      leading: IconButton(
        onPressed: () {},
        icon: Icon(Icons.home),
      ),
    ),
    body: Container(
      child: Center(
        child: Form(
          autovalidateMode: AutovalidateMode.onUserInteraction,
          key: _formKey,
          child: Column(
              mainAxisAlignment: MainAxisAlignment.center,
              crossAxisAlignment: CrossAxisAlignment.center,
              children: <Widget>[
                Row(
                  mainAxisAlignment: MainAxisAlignment.spaceEvenly,
                  crossAxisAlignment: CrossAxisAlignment.center,
                  children: <Widget>[
                    Text("Category"),
                    DropdownButton(
                      hint: Text("Select"),
                      items: _listItem
                          .map<DropdownMenuItem<String>>((valueItem) {
                        return DropdownMenuItem<String>(
                          value: valueItem,
                          child: Text(valueItem),
                        );
                      }).toList(),
                      onChanged: (selectedAccountType) {
                        setState(() {
                          selectedState = selectedAccountType;
                        });
                      },
                      value: selectedState,
                    )
                  ],
                ),
                SizedBox(
                  height: 10,
                ),
                Row(
                  mainAxisAlignment: MainAxisAlignment.spaceEvenly,
                  crossAxisAlignment: CrossAxisAlignment.center,
                  children: <Widget>[
                    Text("SUBCATEGORY"),
                    DropdownButton(
                      hint: Text("Select"),
                      items: _listItem1
                          .map<DropdownMenuItem<String>>((valueItem1) {
                        return DropdownMenuItem<String>(
                          value: valueItem1,
                          child: Text(valueItem1),
                        );
                      }).toList(),
                      onChanged: (selectedAccountType) {
                        setState(() {
                          selectedState = selectedAccountType;
                        });
                      },
                      value: selectedState,
                    )
                  ],
                ),
                SizedBox(height: 10),
                Row(
                  mainAxisAlignment: MainAxisAlignment.spaceEvenly,
                  crossAxisAlignment: CrossAxisAlignment.center,
                  children: <Widget>[
                    Text("MARKED TO"),
                    DropdownButton(
                      hint: Text("Select"),
                      items: _listItem2
                          .map<DropdownMenuItem<String>>((valueItem2) {
                        return DropdownMenuItem<String>(
                          value: valueItem2,
                          child: Text(valueItem2),
                        );
                      }).toList(),
                      onChanged: (selectedAccountType) {
                        setState(() {
                          selectedState = selectedAccountType;
                        });
                      },
                      value: selectedState,
                    )
                  ],
                ),
                SizedBox(height: 10),
                Column(
                  mainAxisAlignment: MainAxisAlignment.center,
                  crossAxisAlignment: CrossAxisAlignment.center,
                  children: [
                    Padding(
                      padding: EdgeInsets.fromLTRB(70, 00, 70, 00),
                      child: TextField(
                        decoration: InputDecoration(
                          hintText: "Describe your problem here.",
                        ),
                        maxLength: 1000,
                        maxLines: 5,
                      ),
                    )
                  ],
                ),
                SizedBox(height: 10),
                ButtonTheme(
                  child: ElevatedButton(
                    child: Text("Submit"),
                    onPressed: () {},
                    style: ElevatedButton.styleFrom(
                      padding: EdgeInsets.symmetric(
                          horizontal: 25, vertical: 15),
                    ),
                  ),
                ),
              ]),
        ),
      ),
    ));
   }
   }

【问题讨论】:

  • 问题是这个变量 selectedState 你在 3 个下拉菜单中使用它们。它也不应该为空

标签: flutter drop-down-menu dropdown flutter-dependencies flutter-test


【解决方案1】:

第一个问题是您为所有DropdownMenu 小部件使用selectedState 变量。这是不切实际的,它可能会导致意外的行为和错误。您应该为每个DropdownMenu 定义一个单独的变量。

另一个问题是您初始化selectedState 变量的方式。 您正在执行此操作:var selectedState;,但未指定值,因此默认情况下它的值为 null

但是,selectedState 变量的初始值必须是DropdownMenu 的选项之一。
例如,如果这些List&lt;String&gt; options = &lt;String&gt;["Option 1", "Option 2", "Option 3", "Option 4"]; 是您对DropdownMenu 的选项,那么您必须像这样初始化selectedState 变量,例如:var selectedState = "Option 1";

错误消失了!

【讨论】:

    猜你喜欢
    • 2022-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-10-27
    • 2021-11-25
    • 1970-01-01
    • 2019-05-25
    • 2021-11-16
    • 2021-05-05
    相关资源
    最近更新 更多