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

  • Dialog Packages

    A Dialog is a class that is intended to be subclassed by other classes. It’s often used to display messages on the page, or make choices available to the user. Flutter has a few built-in dialogs, but there are times when you need to build your own or use an existing package. adaptive_dialog Adaptively display…

  • Introduction Screen Packages

    intro_slider The Flutter Intro Slider is a flutter plugin that helps you create a compelling opening for your app. flutter_intro A more efficient method for introducing new features and a step-by-step user guide for your Flutter project. introduction_screen The Flutter Onboarding/Introduction package for a flutter app with some customization possibilities animated_walkthrough An animated introduction and…

  • Best Flutter Tools

    If you want to get the most out of your experience with this incredible platform then I recommend checking out these awesome tools! Flutter Editors Android Studio Android Studio is an IDE (Integrated Development Environment) for developing a mobile application using Dart/Flutter, Java/Kotlin or C/C++ language on Linux, macOS and Windows operating systems. Visual Studio…

  • Database and ORM Packages

    postgrest The PostgREST client for Dart is a framework developed by the creator of this library. This library offers an ORM interface to PostgREST. conduit A modern HTTP server application framework with OpenAPI 3.0 integration, ORM, and OAuth2 provider. REST, RPC, or GraphQL services are all possible with this foundation. sqfentity_gen The Flutter SDK provides…

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

  • State Manager

    If you are new to Flutter and state management, I suggest using riverpod package. Provider and Riverpod are both good. provider InheritedWidget are snippets of code that allow you to reuse and easily add functionality to widgets. This package adds a wrapper around it so they’re easier to use and more reusable. Too often when…