[Solved] Why can I not check if Flutter list isnotempty?

Question

Asked by Mohammad A on December 05, 2021 (source).

I have created a flutter list but how do I check if it is empty?
List<int> selectedItems = [];
However, selectedItems.isNotEmpty gives me Conditions must have a static type of 'bool'. error. How do I fix this?

I created a dropdown

SearchChoices.multiple(
                        clearIcon: const Icon(FontAwesomeIcons.times),
                        underline: Container(
                          color: const Color(0xffF0F0F0),
                        ),
                        items:
                            _list.map<DropdownMenuItem<String>>((String value) {
                          return DropdownMenuItem<String>(
                            value: value,
                            child: Text(value),
                          );
                        }).toList(),
                        selectedItems: selectedItems,
                        hint: const Padding(
                          padding: EdgeInsets.all(12.0),
                          child: Text("Select any"),
                        ),
                        searchHint: "Select any",
                        onChanged: (value) {
                          setState(() {
                            value = selectedItems;
                          });
                        },
                        closeButton: (selectedItems) {
                          return selectedItems.isNotEmpty
                              ? "Save ${selectedItems.length == 1 ? '"' + items[selectedItems.first].value.toString() + '"' : '(' + selectedItems.length.toString() + ')'}"
                              : "Save without selection";
                        },
                        isExpanded: true,
                      ),

The error is here

closeButton: (selectedItems) {
                          return selectedItems.isNotEmpty
                              ? "Save ${selectedItems.length == 1 ? '"' + items[selectedItems.first].value.toString() + '"' : '(' + selectedItems.length.toString() + ')'}"
                              : "Save without selection";
                        },

Error

The dropdown package I used is: https://pub.dev/packages/searchable_dropdown

UPDATE
It seems that flutter sees the List<int> as a dynamic type. Ever since I began the project it has been assigning every variable as dynamic even if I specified its type. enter image description here Screenshot of variable
enter image description here

Answer

Question answered by Sajad A (source).

Try giving a type to the selectedItems,

According to the searchable_dropdown docs

closeButton: String|Widget|Function with parameter: selectedItems returning String|Widget displayed at the bottom of the search dialog box.

closeButton: (List<int> selectedItems) {
                          return selectedItems.isNotEmpty
                              ? "Save ${selectedItems.length == 1 ? '"' + items[selectedItems.first].value.toString() + '"' : '(' + selectedItems.length.toString() + ')'}"
                              : "Save without selection";
                        },
ARRAYS FLUTTER IF-STATEMENT
SHARE: