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. (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. (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): 
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 = []); }

Similar Posts

  • Packages for Payment Implementation

    These Flutter packages for payment implementation provide everything you need to get started including in-app purchases (IAP) and subscriptions. flutter_stripe_payment To accept card payments, add Stripe to your Flutter application and use Payment Intents with Strong SCA 3DS Compliance. cashfree_pg This is the official Flutter plugin for integrating Cashfree PG with your app. It displays…

  • Text-to-Speech Packages

    Text-to-speech (TTS) is the process of using software to read aloud text that is displayed on a screen. Text-to-speech programs can convert text into speech, and do so in various ways depending on the user’s familiarity with the system. flutter_tts flutter_tts is a good Text to Speech Flutter plugin available on pub.dev platform. This plugin…

  • PDF Packages

    pdf This is a Dart PDF file creator. It can generate pdf files for both the web and Flutter. This library is made up of two parts: a low-level Pdf generation library that manages the creation of the PDF documents. A widgets system comparable to Flutter’s, for easier high-level Pdf production. TrueType fonts can be…

  • Image Libraries

    octo_image The octo_image is a Flutter image widget that may perform various tasks. Placeholders, error indicators, and image transformers with fading are all supported. A placeholder, a progress indicator, an ImageBuilder, or an error widget is required for the OctoImage widget to display the picture. A placeholder or a progress indication may be used, as…

  • GDPR Packages

    GDPR is coming up, and you’re not sure what to do. The General Data Protection Regulation (GDPR) went into effect in 2018. This new regulation will have a major impact on how companies collect data from European Union citizens. It’s important that your company is compliant with this new law or risk heavy fines of…