[SOLVED] Flutter : NoSuchMethodError: The getter ‘isEmpty’ was called on null.
![](https://www.bestdevops.com/wp-content/uploads/2023/05/tr5qv-1-375x210.png)
Limited Time Offer!
For Less Than the Cost of a Starbucks Coffee, Access All DevOpsSchool Videos on YouTube Unlimitedly.
Master DevOps, SRE, DevSecOps Skills!
Problem
I’m Calling An API and getting response in Variable Data ($data). When data is coming then there is no Error but when I’m getting null as response then getting this error Unhandled Exception: NoSuchMethodError: The getter ‘isEmpty’ was called on null.
When I am using the below code then it is throwing me an error:
Solution
That is because $data returns null
, So I’ve Used
if(data?.isEmpty ?? true)
?
prevents an error if the previous part of the expression results in null
and ?? true
results in true
if the previous part of the expression is null
and therefore treats == null
and isEmpty
the same for the if(...)
check.