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

问题描述 投票:0回答:1

我是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>

ProfileForm类:

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);
  }
}
firebase flutter dart google-cloud-firestore
1个回答
1
投票

userProfileData.accountType为'0',而不是'Educator'或'School Administrator'或'Parent'。

成功:值必须在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]值的项目:hahaha

 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;
                      });
                    },
                  ),
© www.soinside.com 2019 - 2024. All rights reserved.