DEV Community

Gülsen Keskin
Gülsen Keskin

Posted on

Mounted check in a stateless widget

If you want to use mounted check in a stateless widget its possible by making an extension on BuildContext

extension ContextExtensions on BuildContext {
  bool get mounted {
    try {
      widget;
      return true;
    } catch (e) {
      return false;
    }
  }
}
Enter fullscreen mode Exit fullscreen mode

and then you can use it like this

if (context.mounted)

Enter fullscreen mode Exit fullscreen mode

https://stackoverflow.com/a/74575955/14745090

Top comments (0)