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 to Scan Barcode and QR

    flutter_barcode_scanner A plugin for Android and iOS that enables barcode scanning. Barcodes, QR codes, and other types of coding are all supported. qr_flutter A Widget or custom painter may be used to display QR codes for Flutter. qr_code_scanner The zxing library is a QR code scanner that can be integrated into flutter. It makes use…

  • Packages for HTTP Request

    http An open-source, Future-based HTTP API that is composable and multi-platform. This bundle has a collection of high-level functions and types that make it simple to access HTTP resources. It’s cross-platform, with support for mobile, desktop, and the browser. dio A robust Dart HTTP client with Interceptors, global configuration, FormData, Request Cancellation, File downloading, and…

  • Clipboard Packages

    A clipboard is one of the most important tools for a mobile phone user. A clipboard is a place where you can store information that you need quick access to or from. You can use those items to copy and paste them into a word processor, mail, chat program, or other function. clipboard This is…

  • Color Pickers

    o_color_picker The package includes a simple and fast two-step color picker that lets you select colors or tones. flex_color_picker The primary, accent, and custom color picker are all editable. An optional HSV wheel color picker is also available. flutter_circle_color_picker A lovely flutter color picker that chooses HSL colors. flutter_colorpicker This is a flutter library that…

  • Animation Packages

    flutter_animate Flutter Animate is a library that makes it simple to add almost any kind of animated effect in Flutter. The library comes with a variety of pre-built effects, like fade, scale, slide, blur, shake, shimmer, and color effects (saturation and tint), that can be easily added to your app. In addition, Flutter Animate simplifies…