Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Check if ListView is empty in flutter

Tags:

flutter

dart

Is there a way to check if ListView is empty.

ListView.builder(
  itemCount: _items.length,
  itemBuilder: (context, index) {
    return _buildFilteredItem(context, index);
  },
)

I want to search through my items and if its empty show a Text widget saying no items found. _buildFilteredItem returns null if the item could not be found.

like image 295
pakoposu Avatar asked Nov 15 '18 16:11

pakoposu


People also ask

How do you know if a list is empty flutter?

How to Check empty list using the length property in the flutter. length property always returns int number. if it is zero , an empty list.

How do you check if a list is null in darts?

List. isEmpty property returns true if the List is empty, or false if it is not. List. isNotEmpty property returns false if the List is empty, or true if it is not.

What is ListView builder in flutter?

ListView is a very important widget in a flutter. It is used to create the list of children But when we want to create a list recursively without writing code again and again then ListView. builder is used instead of ListView. ListView. builder creates a scrollable, linear array of widgets.


1 Answers

Check _items before creating ListView

return _items.isEmpty ? Center(child: Text('Empty')) : ListView.builder(
  itemCount: _items.length,
  itemBuilder: (context, index) {
    return _buildFilteredItem(context, index);
  },
)
like image 162
Andrey Turkovsky Avatar answered Oct 22 '22 18:10

Andrey Turkovsky