DropdownButtonFormField

DropdownButtonFormField

本文介绍了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不存在的地方失败的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-25 19:59