I am having a list presented by a ListView.builder and I use a Function to check if an object in the list is being searched, but I want to present a screen when no object was found. So the user knows that nothing was found but I am strugelling with it, the code looks something like that:
Container OnlyShow = Container(
child: ListView.builder(
itemCount: myList.length,
itemBuilder: (context, index) {
if (searchfunction(myList[index]) == true) {
return myListObject(myList[index]);
} else {
return Container(height: 0, width: 0,);
}
},
),
);
The search function itself is not the problem I only want to know how I could find out if the ListView.builder only returns empty containers, because that would mean that no objects equal the search and therefore nothing was found.
I have tried something like that:
Container OnlyShow = Container(
child: ListView.builder(
itemCount: myList.length,
itemBuilder: (context, index) {
if (searchfunction(myList[index]) == true) {
if (howManyObjectsAreFalse > 0) {
howManyObjectsAreFalse--;
dismissNoResultsFoundScreen();
}
return myListObject(myList[index]);
} else {
if (howManyObjectsAreFalse < myList.length) {
howManyObjectsAreFalse++;
}
if (howManyObjectsAreFalse == myList.length) {
showNoResultsFoundScreen();
}
return Container(height: 0, width: 0,);
}
},
),
);
int howManyObjectsAreFalse = 0;
It should work like this when an empty Container is returned the count is incremented and when a Result is returned the count is decreased, So when the count hits the length of the list the NoResultScreen is being presented.
It does work but with some weird exceptions, sometimes the count seems to fluctuate between to values, even if the ListView.builder only returns empty Containers.
Note: The List is not empty I just return an empty container for every object in the list that is not equal to the search value. So I don't decrease the list or something
I hope someone can help me with a better way to achieve this.
myListremains the same, I dont't changemyListi only look ifsearchfunction(myList) == trueand if i returnmyList[index]and if not i return an empty Containerchild: myList.where(searchfunction).isEmpty ? Text('nothing was found') : ListView.builder(...)Container(height: 0, width: 0,);is wrong: you should remove non existing data, and not showing empty widgetsfinal tempArray = myList.where(searchfunction).toList();? and use it:tempArray.isEmpty? Text('nothing found') : ListView.builder(itemCount: tempArray.length, ...