title: FlutterError (setState() called after dispose(): (lifecycle state: defunct, not mounted)
body: <p>the error is thrown in two areas (and the app freezes (when the app is minimized, when phones back button is clicked, or when another app runs on top of the flutter app. Flutter version: 1.20.2 (previous versions did not have this issue): The two functions are:</p>
  <pre><code>@override
  void initState() {
  super.initState();
  getItems();
  } 
  
  getItems() async {
   initClearVisibility();
   initFilters();
   setState(() {
   loadingItems = true;
   Visibility(visible: true, child: CircularProgressIndicator());
   });
  
  QuerySnapshot querySnapshot = await query.get();
   items = querySnapshot.docs;
   lastDocument = querySnapshot.docs[querySnapshot.docs.length - 1];
   setState(() {
   loadingItems = false;
   Visibility(visible: false, child: CircularProgressIndicator());
   });
  }
  
  initClearVisibility() {
   if (Str.filterSelectCategory != Str.CATEGORY) {
    clearCategoryVisible = true;
    allCategoriesVisible = false;
     categoryValue = Str.filterSelectCategory;
    setState(() {});
   }
  }
  
  initFilters() async {   
   filterDefaultItems();
  }
  
  filterDefaultItems() async {
    query = _firestore
    .collection(Str.ITEMS)
    .where(Str.IS_ITEM_SOLD, isEqualTo: false) 
    .where(Str.ADDRESS, isEqualTo: userAddress1)
    //.orderBy(Str.DATE_POSTED)
    .limit(perPage);
  }
  </code></pre>
  <p>Second area is on the following code where I am also getting:  :</p>
  <pre><code>class FABBottomAppBarItem {
  FABBottomAppBarItem({this.iconData, this.itemColor}); //, this.text});
  IconData iconData;
  var itemColor;
  //String text;
  }
  
  class FABBottomAppBar extends StatefulWidget {
   FABBottomAppBar({
   this.items,
   this.centerItemText,
   this.height: 65.0,
   this.iconSize: 24.0,
   this.backgroundColor,
   this.color,
   this.selectedColor,
   this.notchedShape,
   this.onTabSelected,
   }) {
   assert(this.items.length == 2 || this.items.length == 4);
  }
  final List&lt;FABBottomAppBarItem&gt; items;
  final String centerItemText;
  final double height;
  final double iconSize;
  final Color backgroundColor;
  final Color color;
  final Color selectedColor;
  final NotchedShape notchedShape;
  final ValueChanged&lt;int&gt; onTabSelected;
  
  @override
  State&lt;StatefulWidget&gt; createState() =&gt; FABBottomAppBarState();
  }
  
  class FABBottomAppBarState extends State&lt;FABBottomAppBar&gt; {
  //int _selectedIndex = 0;
  int unreadCount = 0;
  
  _updateIndex(int index) {
   widget.onTabSelected(index);
   setState(() {
    //_selectedIndex = index;
    });
  }
  
  @override
  void initState() {
   super.initState();
   countDocuments();
  }
  
  @override
  Widget build(BuildContext context) {
  List&lt;Widget&gt; items = List.generate(widget.items.length, (int index) {
    return _buildTabItem(
      item: widget.items[index],
      index: index,
      onPressed: _updateIndex,
    );
  });
  items.insert(items.length &gt;&gt; 1, _buildMiddleTabItem());
  
  return BottomAppBar(
    shape: widget.notchedShape,
    child: Row(
      mainAxisSize: MainAxisSize.max,
      mainAxisAlignment: MainAxisAlignment.spaceAround,
      children: items,
    ),
    color: widget.backgroundColor,
   );
  }
  
  Widget _buildMiddleTabItem() {
   return Expanded(
    child: SizedBox(
      height: MediaQuery.of(context).size.height * 0.075, //widget.height,
      child: Column(
        mainAxisSize: MainAxisSize.min,
        mainAxisAlignment: MainAxisAlignment.center,
        children: &lt;Widget&gt;[
          SizedBox(
            height: MediaQuery.of(context).size.height * 0.04,
          ), 
          Text(
            widget.centerItemText ?? '',
            style: TextStyle(
                color: BwerereTheme.bwerereRed,
                fontSize: 14.0,
                fontWeight: FontWeight.w900),
          ),
        ],
      ),
    ),
    );
  }
  
  Widget _buildTabItem({
   FABBottomAppBarItem item,
   int index,
   ValueChanged&lt;int&gt; onPressed,
   }) 
  {
   return Expanded(
    child: SizedBox(
      height: MediaQuery.of(context).size.height * 0.065,
      child: Material(
        type: MaterialType.transparency,
        child: InkWell(
          onTap: () =&gt; onPressed(index),
          child: Column(
            mainAxisSize: MainAxisSize.min,
            mainAxisAlignment: MainAxisAlignment.center,
            children: &lt;Widget&gt;[
              Stack(
                children: &lt;Widget&gt;[
                  Icon(item.iconData,
                      color: item.itemColor,
                      size: IconTheme.of(context).size * 1.2),
                  index == 2 ? badge() : Container()
                ],
              )
            ],
          ),
        ),
      ),
     ),
     );
    }
  
    Widget badge() =&gt; unreadCount &lt; 1
     ? Container()
      : Container(
        padding: EdgeInsets.all(4.0),
        decoration: BoxDecoration(
            color: BwerereTheme.bwerereRed, shape: BoxShape.circle),
        child: Center(
          child: RobotoFont(
              text: &quot;$unreadCount&quot;,
              textSize: 12.0,
              textColor: Colors.white,
              fontWeight: FontWeight.w400),
        ));
  
   void countDocuments() async {
   final uid = await FetchUserData().getCurrentUserID();
   QuerySnapshot _myDoc = await FirebaseFirestore.instance
      .collection(&quot;userUnreadMessages&quot;)
      .doc(uid)
      .collection(Str.MESSAGE_COLLECTION)
      .get();
   List&lt;DocumentSnapshot&gt; _myDocCount = _myDoc.docs;
   setState(() {
    unreadCount = _myDocCount.length;
    print('NOTIY LENGTH::: $unreadCount');
   });
  }
  </code></pre>
  <p>}</p>
  <p>THE ERROR FROM FRAMEWORK.DART for FABBottomAppBarState.</p>
  <p>The same error thrown on the getItems on HomePage()</p>
  <blockquote>
  <p>Exception has occurred.
  FlutterError (setState() called after dispose(): FABBottomAppBarState#250ac(lifecycle state: defunct, not mounted)
  This error happens if you call setState() on a State object for a widget that no longer appears in the widget tree (e.g., whose parent widget no longer includes the widget in its build). This error can occur when code calls setState() from a timer or an animation callback.
  The preferred solution is to cancel the timer or stop listening to the animation in the dispose() callback. Another solution is to check the &quot;mounted&quot; property of this object before calling setState() to ensure the object is still in the tree.
  This error might indicate a memory leak if setState() is being called because another object is retaining a reference to this State object after it has been removed from the tree. To avoid memory leaks, consider breaking the reference to this object during dispose().)</p>
  </blockquote>
  <p>Further investigation then shows that the app takes about 400MB of memory (Ram) for the phone which I find rather too high.</p>
  <p>Help on figuring out the issue will really help. Thanks in advance.</p>
  <p>Additional information:
  Error occurs when running on android 7.0, flutter 1.20.2. See similar/related issue on <a href="https://github.com/flutter/flutter/issues/35900" rel="noreferrer">https://github.com/flutter/flutter/issues/35900</a>. Note that I upgraded to Flutter 1.20.2 adnd Downgrading to 1.7.5 will require a lot of changes I made after upgrading especially on Firestore (NOTE: <a href="https://firebase.flutter.dev/docs/migration" rel="noreferrer">https://firebase.flutter.dev/docs/migration</a> which was recently updated).</p>