断言失败:第1252行,第12个位置:'widget.items!.where((DropdownMenuItem <T> item)=> item.value == widget.value)。length == 1':不是真的。

3
当我尝试使用Flutter DropdownButton Widget时,控制台中出现了以下错误:

package:flutter/src/material/dropdown.dart': Failed assertion: line 1252 pos 12: 'widget.items!.where((DropdownMenuItem item) => item.value == widget.value).length == 1': is not true.

有一个很长的回溯信息......这里我添加了一个小代码示例,可以在main.dart文件中简单地复制粘贴以重现此错误。

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

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

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

  @override
  State<BugReportApp> createState() => _BugReportAppState();
}

class _BugReportAppState extends State<BugReportApp> {
  final TextEditingController _dropdownController = TextEditingController();
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      title: 'Bug Report',
      theme: ThemeData(
        primarySwatch: Colors.blue,
      ),
      home: Flex(direction: Axis.vertical, children:[
                        DropdownButton<String>(
            value: _dropdownController.text == ""
                ? null
                : _dropdownController.text,
            items: ["hello, world", "how are you", "goodbye"]
                .map((_value) => DropdownMenuItem<String>(
                        child: Text(
                      _value,
                    )))
                .toList(),
            onChanged: (_value) {
              setState(() {
                _dropdownController.text = _value ?? _dropdownController.text;
              });
            },
          ),
      ],),
    );
  }
}

我本以为下拉菜单会正常工作,但不知道为什么它没有正常工作。
3个回答

9
您在 DropdownMenuItem 中缺少 value
.map((_value) => DropdownMenuItem<String>(
        value: _value, // this
        child: Text(
          _value,
        )))

同时确保在主页上使用Scaffold


3

试试这段代码,同时在代码中加入了一些解释:

class _MyHomePageState extends State<MyHomePage> {

  final TextEditingController _dropdownController = TextEditingController();

  String? dropDownValue = 'hello, world'; // add one value as the defaul one which must exists in the dropdown value

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      backgroundColor: Colors.white,
      body: Column(
          children: [

            Flex(direction: Axis.vertical, children:[
              DropdownButton<String>(
                value: dropDownValue, // this place should not have a controller but a variable
                onChanged: (_value) {
                  setState(() {
                    dropDownValue = _value;
                  });
                },
                items: ["hello, world", "how are you", "goodbye"]
                    .map<DropdownMenuItem<String>>((String _value) => DropdownMenuItem<String>(
                    value: _value, // add this property an pass the _value to it
                    child: Text(_value,)
                )).toList(),
              ),
            ])

          ],
        ),

    );
  }

}

1
你也可以在 DropdownButtonvalue 参数中传递 null,它可以接受 null。 - Yeasin Sheikh

1

请在DropdownMenuItemDropdownButton中都添加VALUE字段以避免错误。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接