Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to check if Widget has mounted in flutter

Tags:

flutter

dart

So in my app, I want to make an Ajax request as soon as the widget is mounted, not in initState(). Similar to ComponentWillMount() in react

like image 698
Kingsley CA Avatar asked Feb 03 '19 08:02

Kingsley CA


1 Answers

if the Widget has not mounted then return. Do it before the setState method

if (!mounted) return;
setState(() {});

or

if (mounted) {
   //Do something
};
setState(() {});

like image 66
Classy-Bear Avatar answered Sep 28 '22 06:09

Classy-Bear