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

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

  • Text Packages

    One of the benefits of using Flutter is its rich graphical system, which makes it easy for developers to make their app look great with very little effort. To help you get started, we’ve put together this roundup of some of our favorite packages and libraries for creating beautiful text layouts with Flutter! flutter_typeahead A…

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

  • Bottom Bar Packages

    convex_bottom_bar The ConvexAppBar is a Flutter package that creates and shows a convex tab in the bottom bar. Theming is supported. persistent_bottom_nav_bar For Flutter, a complete, highly configurable bottom navigation bar that is persistent/static and can be used for any application. It has up to 20 different styles. salomon_bottom_bar Another bottom navigation bar, but this…

  • Packages for Excel and Word

     These packages allow developers to create beautiful charts in Excel or add interactive features to Word documents like adding hyperlinks or creating tables. They are all free and open source so you can use them on your next project! excel This is a Flutter and Dart library for reading, creating, editing, and updating Excel sheets…