Unhandled Exception: This widget has been unmounted, so the State no longer has a context

You will encounter the following error when updating data with setState method after the display widget is disposed.

E/flutter (30970): [ERROR:flutter/lib/ui/ui_dart_state.cc(209)] Unhandled Exception: This widget has been unmounted, so the State no longer has a context (and should be considered defunct).
E/flutter (30970): Consider canceling any active work during "dispose" or using the "mounted" getter to determine if the State is still active.
E/flutter (30970): #0      State.context.<anonymous closure> (package:flutter/src/widgets/framework.dart:909:9)
E/flutter (30970): #1      State.context (package:flutter/src/widgets/framework.dart:915:6)
E/flutter (30970): #2      _ProductsPageState._fetchProducts.<anonymous closure> (package:example/screens/product/products_screen.dart:55:9)
E/flutter (30970): #3      _rootRunUnary (dart:async/zone.dart:1436:47)
E/flutter (30970): #4      _CustomZone.runUnary (dart:async/zone.dart:1335:19)
E/flutter (30970): <asynchronous suspension>
E/flutter (30970): 

To fix this issue, you need to check whether the widget is mounted. Then only call setState method if it is true.


if (mounted) { 
  setState (() => _products = []);
}

By continuing to use the site, you agree to the use of cookies. more information

The cookie settings on this website are set to "allow cookies" to give you the best browsing experience possible. If you continue to use this website without changing your cookie settings or you click "Accept" below then you are consenting to this.

Close