How to check for an list array that has more than 1 String

Viewed 28

I have a array here and I want to check If in that array I have more that 1 string or not as describe in code:

 bool? checkEmpty1 = false;

 StreamBuilder<DocumentSnapshot<Map<String, dynamic>>>(
                    stream: FirebaseFirestore.instance
                        .collection('groups')
                        .doc(groupId)
                        .snapshots(),
                    builder: (context, snapshot2) {
                      snapshot2.data?.data()?.forEach((key, value) {
                        if (key == 'members') {
                          checkEmpty1 = value == '';
                        }
                      });
           //I want to do like If members is more than 1 show this If members == to 1 show this
                         return checkEmpty1!
                                ? const Text('Has one members')
                                : const Text('Has more than one members')

picture:enter image description here

1 Answers
// declare variable which will count the number of String in the list
  int stringCount = 0;

  if (checkEmpty1 != null) {
    for (int i = 0; i < checkEmpty1.length; i++) {
      if (checkEmpty1[i] is String) {
// increase the string count by 1
        stringCount++;
      }
//now you can check the stringCount count to determine the number of string in the list
      if (stringCount > 1) {
//this will know that there are more than one String in the list, 
        return;
      }
    }
  }
Related