检测到零个或 2 个或多个 [DropdownMenuItem] 具有相同的值

2023-12-21

我是 Flutter 新手,但我正在尝试创建一个 DropdownButtonFormField 但它不起作用。我收到一条错误消息,提示我有重复的值。有趣的是,我没有包含重复值的列表。我在 SO 上发现了一个类似的问题,解决方案说用一个值启动字符串,并且用户正在复制一个列表项,但我对另一个列表有类似的解决方案,它似乎工作正常。我不明白为什么它在这里不起作用。任何帮助是极大的赞赏。

错误信息:

There should be exactly one item with [DropdownButton]'s value: 0. 
Either zero or 2 or more [DropdownMenuItem]s were detected with the same value
'package:flutter/src/material/dropdown.dart':
Failed assertion: line 1411 pos 15: 'items == null || items.isEmpty || value == null ||
              items.where((DropdownMenuItem<T> item) {
                return item.value == value;
              }).length == 1'
The relevant error-causing widget was: 
  StreamBuilder<UserProfile>

个人资料表格类:

final List<String> accountType = ['Educator', 'School Administrator', 'Parent'];

  String _currentAccountType;

  @override
  Widget build(BuildContext context) {
    final user = Provider.of<User>(context);
    return StreamBuilder<UserProfile>(
        stream: DatabaseService(uid: user.uid).userProfileData,
        builder: (context, snapshot) {
          if (snapshot.hasData) {
            UserProfile userProfileData = snapshot.data;
            return Form(
              key: _formKey,
              child: Column(
                children: <Widget>[
                  SizedBox(height: 20.0),
                  DropdownButtonFormField(
                    decoration: textInputDecoration,
                    value: _currentAccountType ?? userProfileData.accountType,
                    items: accountType.map((accountType) {
                      return DropdownMenuItem(
                        value: accountType,
                        child: Text(accountType),
                      );
                    }).toList(),
                    onChanged: (val) {
                      setState(() {
                        _currentAccountType = val;
                      });
                    },
                  ),

数据库类

class DatabaseService {
  final String uid;
  DatabaseService({this.uid});

  final CollectionReference userProfileCollection =
      Firestore.instance.collection('user_profile');

  Future updateUserProfile(
      String accountType,
      String birthDate,
      String courseName,
      String dateJoined,
      String email,
      String firstName,
      String lastName,
      String schoolName,
      String title) async {
    return await userProfileCollection.document(uid).setData({
      'accountType': accountType,
      'birthDate': birthDate,
      'courseName': courseName,
      'dateJoined': dateJoined,
      'email': email,
      'firstName': firstName,
      'lastName': lastName,
      'schoolName': schoolName,
      'title': title,
    });
  }

  //User Profile from snapshot
  List<Profile> _userProfileListFromSnapshot(QuerySnapshot snapshot) {
    return snapshot.documents.map((doc) {
      return Profile(
        accountType: doc.data['accountType'] ?? '',
        birthDate: doc.data['birthDate'] ?? '',
        courseName: doc.data['courseName'] ?? '',
        dateJoined: doc.data['dateJoined'] ?? '',
        email: doc.data['email'] ?? '',
        firstName: doc.data['firstName'] ?? '',
        lastName: doc.data['lastName'] ?? '',
        schoolName: doc.data['schoolName'] ?? '',
        title: doc.data['title'] ?? '',
      );
    }).toList();
  }

  UserProfile _userProfileFromSnapshot(DocumentSnapshot snapshot) {
    return UserProfile(
      uid: uid,
      accountType: snapshot.data['accountType'],
      birthDate: snapshot.data['birthDate'],
      courseName: snapshot.data['courseName'],
      dateJoined: snapshot.data['dateJoined'],
      email: snapshot.data['email'],
      firstName: snapshot.data['firstName'],
      lastName: snapshot.data['lastName'],
      schoolName: snapshot.data['schoolName'],
      title: snapshot.data['title'],
    );
  }

  Stream<List<Profile>> get userProfile {
    return userProfileCollection.snapshots().map(_userProfileListFromSnapshot);
  }

  Stream<UserProfile> get userProfileData {
    return userProfileCollection
        .document(uid)
        .snapshots()
        .map(_userProfileFromSnapshot);
  }
}

userProfileData.accountType 为“0”,而不是“教育者”、“学校管理员”或“家长”。

成功:值必须在 items.value 中

 final List<String> accountType = ['Educator', 'School Administrator', 'Parent'];

 DropdownButtonFormField(
                    decoration: textInputDecoration,
                    value: accountType[0],
                    items: accountType.map((accountType) {
                      return DropdownMenuItem(
                        value: accountType,
                        child: Text(accountType),
                      );
                    }).toList(),
                    onChanged: (val) {
                      setState(() {
                        _currentAccountType = val;
                      });
                    },
                  ),

失败:应该只有一项具有 [DropdownButton] 的值:哈哈哈

 final List<String> accountType = ['Educator', 'School Administrator', 'Parent'];

 DropdownButtonFormField(
                    decoration: textInputDecoration,
                    value: 'hahaha',
                    items: accountType.map((accountType) {
                      return DropdownMenuItem(
                        value: accountType,
                        child: Text(accountType),
                      );
                    }).toList(),
                    onChanged: (val) {
                      setState(() {
                        _currentAccountType = val;
                      });
                    },
                  ),
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

检测到零个或 2 个或多个 [DropdownMenuItem] 具有相同的值 的相关文章

随机推荐

  • 在三元运算符中使用 return

    我尝试在三元运算符中使用 return 但收到错误 Parse error syntax error unexpected T RETURN 这是代码 e this gt return errors e return array false
  • 如何在不使用
    的情况下从 css 换行?

    如何在不使用的情况下实现相同的输出 br p hello br How are you p output hello How are you 您可以使用white space pre https developer mozilla org
  • 这个Array slice调用的原型,为什么呢?

    我正在阅读 JS Function 的 MDN 页面论点多变的 https developer mozilla org en JavaScript Reference Functions and function scope argumen
  • 在 PHP 中使用 preg_grep() 查找字符串之前或之后的字符

    我需要使用 PHP 查找单词之前或之后的任何字符preg grep 来自数组 我有一个如下所示的数组 findgroup array aphp phpb dephpfs potatoes 我需要从数组中查找在单词 php 之前或之后 两侧
  • TotalResults 计数与 YouTube v3 搜索 API 返回的实际结果不匹配

    我们正在使用 youtube v3 搜索 API 我们发现 totalResults 计数与response items 字段中返回的项目列表不匹配 我在请求中请求 50 个视频 返回的响应显示 TotalResults 计数为 65 但响
  • 请解释一下 Amazon RDS/Mysql 中的这种内存消耗模式?

    Folks 有人可以解释运行 Mysql 的 Amazon RDS 上的这种内存消耗模式吗 在此图中 我在 03 30 升级到了 db m2 2xlarge 具有 34GB 可用内存 您可以非常清楚地看到切换 当客户端开始连接并访问该实例时
  • 空间数据类型(几何)到 GeoJSON

    我想转换geom geometry 数据类型转换为 GeoJSON 我怎么能这么做呢 例如 WKT 中的几何图形 POLYGON 455216 346127297 4288433 28426224 455203 386722146 4288
  • 在 Groovy 中动态添加元素到 ArrayList

    我是 Groovy 的新手 尽管阅读了很多有关此的文章和问题 但我仍然不清楚发生了什么 据我目前的了解 当您在 Groovy 中创建一个新数组时 底层类型是 Java ArrayList 这意味着它应该是可调整大小的 您应该能够将其初始化为
  • 如何防止遗传算法收敛于局部极小值?

    我正在尝试使用遗传算法构建 4 x 4 数独求解器 我对值收敛到局部最小值有一些问题 我正在使用排名方法并删除排名底部的两个答案可能性 并将它们替换为排名最高的两个答案可能性之间的交叉 为了获得避免局部最小值的额外帮助 我还使用了突变 如果
  • 文字字符串 [Lua 5.1]

    所以我开始学习Lua 5 1 我看到了一个叫做文字字符串的东西 我不知道这些是做什么的 手册上说 a 是一个铃声 但是当我输入时 print hello athere IDE 打印一个奇怪的正方形 上面写着 bel 因此 如果有人可以帮助我
  • 为什么结构标签不是 C 中的类型名称? [关闭]

    就目前情况而言 这个问题不太适合我们的问答形式 我们希望答案得到事实 参考资料或专业知识的支持 但这个问题可能会引发辩论 争论 民意调查或扩展讨论 如果您觉得这个问题可以改进并可能重新开放 访问帮助中心 help reopen questi
  • 有人知道一个好的 MSI 日志查看器吗?

    相当简单的问题 有谁知道浏览 msi 日志文件的好实用程序吗 对任何提供过滤 不同标准和自定义操作 操作排序 属性和错误的良好视图的事物感兴趣 Thanks I found Rob Mensching 关于在 MSI 日志中查找的第一件事的
  • 如何使用 ReactJS 在 CKEditor 5 中使用 MathType 插件?

    我已经安装了三个包 ckeditor ckeditor5 react ckeditor ckeditor5 build classic wiris mathtype ckeditor5 src plugin 我可以设置简单的 ckedito
  • 如何优化具有数千个 WHERE 子句的 SQL 查询

    我对一个非常大的数据库进行了一系列查询 并且 WHERE 子句中有数十万个 OR 优化此类 SQL 查询的最佳且最简单的方法是什么 我找到了一些有关创建临时表和使用联接的文章 但我不确定 我是严肃 SQL 的新手 并且一直在将结果从一个SQ
  • iOS 15+ contextMenu 中的反向 ScrollView 错误

    感谢您花时间帮助他人 例如 我已经检查了所有解决方案这个帖子 https stackoverflow com questions 61726424 swiftui chat app the woes of reversed list and
  • 泛型:为什么编译器在这种情况下无法推断类型参数?

    我想编写一个扩展方法 该方法适用于其值是某种序列的字典 不幸的是 编译器似乎无法从我对该方法的使用中推断出通用参数 我需要明确指定它们 public static void SomeMethod
  • 找不到模块 java.xml.bind

    我是新来的javafx和日食 我从 eclipse market 安装了 eclipse 然后 javafx 我使用场景生成器生成了 fxml 代码 但无法执行它 我真的很受阻 找不到任何解决方案 I added add modules j
  • jQuery Datatables - 从其他页面检索信息

    我在从 jQuery 数据表获取信息时遇到问题 这是表格 我想获取表中存储的信息 我尝试通过以下方式做到这一点 var languages var people select name languageID each function la
  • 确定 WindowsIdentity 实例的嵌套组

    假设我有一个实例WindowsIdentity并想要获取它所属的组 我使用以下代码来获取列表 WindowsIdentity identity null get identity here identity Groups Translate
  • 检测到零个或 2 个或多个 [DropdownMenuItem] 具有相同的值

    我是 Flutter 新手 但我正在尝试创建一个 DropdownButtonFormField 但它不起作用 我收到一条错误消息 提示我有重复的值 有趣的是 我没有包含重复值的列表 我在 SO 上发现了一个类似的问题 解决方案说用一个值启