flutter Assert失败:第1252行位置12:'窗口小部件.项目!.where((下拉菜单项目)=>项目.值==窗口小部件.值).长度== 1'< T>:不是真的

n53p2ov0  于 2022-12-19  发布在  Flutter
关注(0)|答案(3)|浏览(153)

当我尝试使用flutter DropdownButton控件时,在控制台中出现此错误。
Package :flutter/源代码/材质/下拉菜单。dart ':Assert失败:第1252行位置12:'小部件.项目!. where((下拉菜单项目)=〉项目.值==小部件.值).长度== 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能正常工作,但我不知道为什么它不能。

6rqinv9w

6rqinv9w1#

DropdownMenuItem上缺少value

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

同时确保在家里使用Scaffold

ih99xse1

ih99xse12#

试试这段代码,在代码中也加了一些解释:

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

          ],
        ),

    );
  }

}
nkhmeac6

nkhmeac63#

请在下拉菜单项下拉按钮中添加 * 值 * 字段以防止错误

相关问题