1

I have a flutter app that checks a list, and depending on what the if else does, something pops up on the screen. I have an if that checks if the list is empty or its length equal to 0, an else that if the list has content, it shows on the screen. But in case the list has size, but its contents are null, and so nothing is built on the screen, what I want to do is return a Text() widget for example, which says 'nothing was found'. How can I treat this problem?

      if (colors == null || colors.length == 0)
        Center(
          child: Padding(
            padding: EdgeInsets.fromLTRB(40, 40, 40, 0),
            child: CircularProgressIndicator(),
          ),
        )
      else if() //What should I do here?
      //emptyState
      else ...colorsList()
Kaya Ryuuna
  • 554
  • 2
  • 4
  • 18
  • What type is your `List` of colors? It seems kinda odd if you are allowing the list to contain `null` objects. What would the purpose of that be? – julemand101 Jul 16 '21 at 19:22
  • It's a dynamic list, I have the String name, and double colorID. – Kaya Ryuuna Jul 16 '21 at 19:38
  • Being dynamic doesn't mean that you need to allow `null` to be added to the `List`. If you do actually need `null` elements, then I would just [filter it to remove `null` elements first](https://stackoverflow.com/q/66896648/). – jamesdlin Jul 16 '21 at 19:54

1 Answers1

4

You could do something like

if(colors?.where((e) => e != null)?.toList()?.isEmpty ?? true) {
    //do your thing when list or it's content is empty
}
Rahul
  • 4,699
  • 5
  • 26
  • 38