断言失败:第 1252 行 pos 12:'widget.items!.where((DropdownMenuItemitem) => item.value == widget.value).length == 1':不是 true

发布于 2025-01-18 11:27:45 字数 1632 浏览 1 评论 0原文

当我尝试使用Flutter Latpdownbutton窗口小部件时,我会在控制台中遇到此错误。

软件包:Flutter/src/材料/下拉':DART':失败断言:第1252 POS 12:'widget.items!。 == 1':不正确。

有很长的追溯... 在这里,我正在添加将重现此错误的小型代码样本...任何人都可以简单地复制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;
              });
            },
          ),
      ],),
    );
  }
}

我期望dropown正常工作,但是,我不知道为什么它不' t。

I am getting this error in the console when I am trying to use 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.

There is a long traceback...
Here I am adding small code sample that will reproduce this error... Anyone can simply copy paste in main.dart file


// 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;
              });
            },
          ),
      ],),
    );
  }
}

I was expecting dropown to work normally but, I don't know why it didn't.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

姐不稀罕 2025-01-25 11:27:45

您在 DropdownMenuItem 上缺少 value

.map((_value) => DropdownMenuItem<String>(
        value: _value, // this
        child: Text(
          _value,
        )))

还要确保在家里使用Scaffold

You are missing value on DropdownMenuItem.

.map((_value) => DropdownMenuItem<String>(
        value: _value, // this
        child: Text(
          _value,
        )))

Also make sure to use Scaffold on home.

谁人与我共长歌 2025-01-25 11:27:45

尝试此代码,还添加了代码中的一些说明:

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(),
              ),
            ])

          ],
        ),

    );
  }

}

Try this code, also added some explanation in the code:

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(),
              ),
            ])

          ],
        ),

    );
  }

}
笙痞 2025-01-25 11:27:45

请在下拉>中添加 value 字段

please add the VALUE field in both DropdownMenuItem and DropdownButton to prevent error

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文