DropdownButtonFormField断言在DropdownButtonHideUnderline不存在的地方失败

编程入门 行业动态 更新时间:2024-10-24 18:17:54
本文介绍了DropdownButtonFormField断言在DropdownButtonHideUnderline不存在的地方失败的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

这适用于DropdownButtonHideUnderline,但不适用于DropdownButtonFormField。我想要通过DropdownButtonFormField获得的inputDecoration,但是当我更改项目时此代码在运行时失败。

This works with DropdownButtonHideUnderline, but does not work with DropdownButtonFormField. I want the inputDecoration that I get with DropdownButtonFormField, but this code fails at runtime when I change the project.

我需要修复它以便与DropdownButtonFormField一起运行,或者我应该找到一种方法得到inputDecoration添加到DropdownButtonHideUnderline;

I either need to fix it to run with DropdownButtonFormField or I should find a way to get the inputDecoration added to the DropdownButtonHideUnderline;

在运行时出现的错误是:

At runtime the error that comes out is:

Either zero or 2 or more [DropdownMenuItem]s were detected with the same value 'package:flutter/src/material/dropdown.dart': Failed assertion: line 827 pos 15: 'items == null || items.isEmpty || value == null || items.where((DropdownMenuItem<T> item) { return item.value == value; }).length == 1'

import 'package:flutter/material.dart'; void main() { runApp(MyApp()); } class MyApp extends StatelessWidget { @override Widget build(BuildContext context) { return MaterialApp( title: 'Flutter Demo', theme: ThemeData( primarySwatch: Colors.blue, visualDensity: VisualDensity.adaptivePlatformDensity, ), home: MyHomePage(title: 'Flutter Demo Home Page'), ); } } class MyHomePage extends StatefulWidget { MyHomePage({Key key, this.title}) : super(key: key); final String title; @override _MyHomePageState createState() => _MyHomePageState(); } Map data = { 'Project 1': ['Entrance', 'Main Hallway', 'Kitchen'], 'Project 2': ['Patio', 'Dining Room'], }; class _MyHomePageState extends State<MyHomePage> { String _project; String _room; List<String> _roomList = []; @override Widget build(BuildContext context) { return Scaffold( appBar: AppBar( title: Text(widget.title), ), body: Center( child: Column( children: <Widget>[ DropdownButtonFormField( decoration: InputDecoration(labelText: 'Project'), value: _project, onChanged: (value) { setState(() { _project = value; _room = null; _roomList = data[_project]; }); }, items: data.keys.map((item) { return DropdownMenuItem( child: Text(item), value: item, ); })?.toList() ?? [], ), DropdownButtonFormField( decoration: InputDecoration(labelText: 'Room'), value: _room, onChanged: (value) { setState(() { _room = value; print(_project); print(_room); }); }, items: _roomList.map((item) { return DropdownMenuItem( child: Text(item), value: item, ); })?.toList() ?? [], ), ], ), )); } }

推荐答案

您可以在 下面复制粘贴运行完整代码 当下拉列表数据完全不同时,将触发此错误 对于房间 DropdownButtonFormField 您可以使用 key:UniqueKey(),小部件将重新创建

You can copy paste run full code below When Dropdown list data is totally different will trigger this error For Room DropdownButtonFormField You can use key: UniqueKey() and widget will recreate

代码段

DropdownButtonFormField( key: UniqueKey(), decoration: InputDecoration(labelText: 'Room'),

工作演示

完整代码

import 'package:flutter/material.dart'; void main() { runApp(MyApp()); } class MyApp extends StatelessWidget { @override Widget build(BuildContext context) { return MaterialApp( title: 'Flutter Demo', theme: ThemeData( primarySwatch: Colors.blue, visualDensity: VisualDensity.adaptivePlatformDensity, ), home: MyHomePage(title: 'Flutter Demo Home Page'), ); } } class MyHomePage extends StatefulWidget { MyHomePage({Key key, this.title}) : super(key: key); final String title; @override _MyHomePageState createState() => _MyHomePageState(); } Map data = { 'Project 1': ['Entrance', 'Main Hallway', 'Kitchen'], 'Project 2': ['Patio', 'Dining Room'], }; class _MyHomePageState extends State<MyHomePage> { String _project; String _room; List<String> _roomList = []; @override Widget build(BuildContext context) { return Scaffold( appBar: AppBar( title: Text(widget.title), ), body: Center( child: Column( children: <Widget>[ DropdownButtonFormField( decoration: InputDecoration(labelText: 'Project'), value: _project, onChanged: (value) { setState(() { _project = value; _room = null; _roomList = data[_project]; }); }, items: data.keys.map((item) { return DropdownMenuItem( child: Text(item), value: item, ); })?.toList() ?? [], ), DropdownButtonFormField( key: UniqueKey(), decoration: InputDecoration(labelText: 'Room'), value: _room, onChanged: (value) { setState(() { _room = value; print(_project); print(_room); }); }, items: _roomList.map((item) { return DropdownMenuItem( child: Text(item), value: item, ); })?.toList() ?? [], ), ], ), )); } }

更多推荐

DropdownButtonFormField断言在DropdownButtonHideUnderline不存在的地方失败

本文发布于:2023-10-18 10:44:42,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1504002.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:不存在   断言   地方   DropdownButtonFormField   DropdownButtonHideUnderline

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!