mercredi 17 mars 2021

flutter-firestore: how to check if the same firestore value is selected

In my flutter-firestore app, users can choose their service fields multiple times. If a user chose a few service fields already, and is trying to choose another field, I want to check if the user is choosing the same field and prevent him from choosing the same field as already chosen ones. I wrote like the following, but it doesn't work. What is wrong with my code? Also, If there is a better way to do this, please give me some comments.

 buildDropdownButtonFormField() {
 final DocumentReference documentReference =
    FirebaseFirestore.instance.collection('users').doc(user.uid);
 documentReference.snapshots().listen((snapshot) {
  if (snapshot.exists && snapshot.data().containsKey('fieldsChosen')) {
   List list = snapshot.data()['fieldsChosen'];
    return Column(children: [
      if (index == 0)
        DropdownButtonFormField(
            items: fields1.map((e) {
              return DropdownMenuItem(value: e, child: Text('$e'));
            }).toList(),
            onChanged: (val) {
              list.contains(val) ? return Text('Please choose a new one')
              : setState(() {
                  _currentfield = val;
                }))
      else if (index == 1)
        DropdownButtonFormField(
           items: fields2.map((e) {
              return DropdownMenuItem(value: e, child: Text('$e'));
            }).toList(),
            onChanged: (val) {
               list.contains(val) ? return Text('Please choose a new one')
              : setState(() {
                _currentfield = val;}
                }))

Aucun commentaire:

Enregistrer un commentaire