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

  • Date and Calendar Packages

    date_format The date_format is an easy-to use API that provides several constants for building your own Date format string. Use the `formatDate` method to create customised dates, and be sure they look good on any device! It provides various constants that you can use when building your Date string with the formatDate method, so it’s…

  • Shared Preferences Packages

    shared_preferences The Flutter plugin for reading and writing simple key-value pairs wraps NSUserDefaults on iOS and SharedPreferences on Android. rx_shared_preferences Preference updates for the rxdart streaming service are communicated via Reactive Streams. The SharedPreference wrapper is reactive stream rxdart. A stream-based wrapper around shared preferences that allows for reactively stored key-value pairs. shared_preferences_dtx The shared…

  • 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…

  • Packages to Work with Firebase

    firebase Web and server libraries for Dart’s Firebase framework firebase_core The Firebase SDK for Flutter allows you to link your application to many different Firebase applications. cloud_functions A Firebase Cloud Functions plugin for Flutter. fcm_config This flutter package integrates firebase_messaging and flutter_local_notifications. firebase_auth_oauth OAuth sign-in flows are made easy with this Flutter plugin that uses…

  • Printer Packages

    blue_thermal_printer This Flutter plugin for connecting to a thermal printer via bluetooth is still in its early stages of development, but it’s already available. bluetooth_thermal_printer The library supports both 58mm and 80mm Bluetooth printers. It uses a Bluetooth printer (Android Only). It works with both 58mm and 80mm Bluetooth printers. This library does not require…