🚀 DevOps Certified Professional
📅 Starting: 1st of Every Month 🤝 +91 8409492687 | 🤝 +1 (469) 756-6329 🔍 Contact@DevOpsSchool.com

[SOLVED] Flutter : NoSuchMethodError: The getter ‘isEmpty’ was called on null.

DevOps

Upgrade & Secure Your Future with DevOps, SRE, DevSecOps, MLOps!

We spend hours on Instagram and YouTube and waste money on coffee and fast food, but won’t spend 30 minutes a day learning skills to boost our careers.
Master in DevOps, SRE, DevSecOps & MLOps!

Learn from Guru Rajesh Kumar and double your salary in just one year.


Get Started Now!

Problem

When I call an API, the response comes back as Variable Data ($data). When data arrives, there is no error; but, when I receive null as a response, I receive the error Unhandled. Exception: The getter ‘isEmpty’ was called on null, resulting in a NoSuchMethodError.

The code below is giving me an error when I use it:

Solution

Due to the fact that $data returns null, I’ve used

If (data?.isEmpty?? true), then

? avoids an error if the expression’s first component is null, and?? Since isEmpty and == null are treated equally for the purposes of the if(…) check, true results in true if the previous component of the expression is null.

My code is now correctly running.

Subscribe
Notify of
guest


This site uses Akismet to reduce spam. Learn how your comment data is processed.

0 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x