title: Error: '_elements.contains(element)': is not true
body: <p>I am seeing this error <code> '_elements.contains(element)': is not true.</code>for the past two days in my flutter project, this was working absolutely fine since the beginning. I am sorry to say this but I have no clue regarding this error but i think i am missing something which is causing this error. Also, when I hot reload the same page the layout is populated, but throws the exceptions.</p>
  <p>Intermittently this was also showing Duplicate GlobalKeys() detected, but this wasn't the case in the beginning.</p>
  <p>Please can someone help me if they stumbled upon these errors before. I couldn't really write an example to reproduce the same problem. Many thanks.</p>
  <p>I have given the error details as follows:</p>
  <pre><code>Performing hot reload...
  I/flutter (22248): ══╡ EXCEPTION CAUGHT BY WIDGETS LIBRARY 
  ╞═══════════════════════════════════════════════════════════
  I/flutter (22248): The following assertion was thrown building 
  TickerMode(mode: disabled):
  I/flutter (22248): 'package:flutter/src/widgets/framework.dart': Failed 
  assertion: line 1766 pos 12:
  I/flutter (22248): '_elements.contains(element)': is not true.
  I/flutter (22248): 
  I/flutter (22248): Either the assertion indicates an error in the framework 
  itself, or we should provide substantially
  I/flutter (22248): more information in this error message to help you 
  determine and fix the underlying cause.
  I/flutter (22248): In either case, please report this assertion by filing a 
  bug on GitHub:
  I/flutter (22248):   https://github.com/flutter/flutter/issues/new
  I/flutter (22248): 
  I/flutter (22248): When the exception was thrown, this was the stack:
  I/flutter (22248): #2      _InactiveElements.remove 
   (package:flutter/src/widgets/framework.dart:1766:12)
  I/flutter (22248): #3      Element._retakeInactiveElement 
  (package:flutter/src/widgets/framework.dart:2850:29)
  I/flutter (22248): #4      Element.inflateWidget 
  (package:flutter/src/widgets/framework.dart:2873:32)
  I/flutter (22248): #5      Element.updateChild 
  (package:flutter/src/widgets/framework.dart:2688:12)
  I/flutter (22248): #6      ComponentElement.performRebuild 
  (package:flutter/src/widgets/framework.dart:3557:16)
  I/flutter (22248): #7      Element.rebuild 
  (package:flutter/src/widgets/framework.dart:3446:5)
  I/flutter (22248): #8      ProxyElement.update 
  (package:flutter/src/widgets/framework.dart:3792:5)
  I/flutter (22248): #9      Element.updateChild 
  (package:flutter/src/widgets/framework.dart:2677:15)
  I/flutter (22248): #10     RenderObjectElement.updateChildren 
  (package:flutter/src/widgets/framework.dart:4262:32)
  I/flutter (22248): #11     _TheatreElement.update 
  (package:flutter/src/widgets/overlay.dart:508:17)
  I/flutter (22248): #12     Element.updateChild 
  (package:flutter/src/widgets/framework.dart:2677:15)
  I/flutter (22248): #13     ComponentElement.performRebuild 
  (package:flutter/src/widgets/framework.dart:3557:16)
  I/flutter (22248): #14     Element.rebuild 
  (package:flutter/src/widgets/framework.dart:3446:5)
  I/flutter (22248): #15     StatefulElement.update 
  (package:flutter/src/widgets/framework.dart:3682:5)
  I/flutter (22248): #16     Element.updateChild 
  (package:flutter/src/widgets/framework.dart:2677:15)
  I/flutter (22248): #17     ComponentElement.performRebuild 
  (package:flutter/src/widgets/framework.dart:3557:16)
  I/flutter (22248): #18     Element.rebuild 
  (package:flutter/src/widgets/framework.dart:3446:5)
  I/flutter (22248): #19     ProxyElement.update 
  (package:flutter/src/widgets/framework.dart:3792:5)
  I/flutter (22248): #20     Element.updateChild 
  (package:flutter/src/widgets/framework.dart:2677:15)
  I/flutter (22248): #21     SingleChildRenderObjectElement.update 
  (package:flutter/src/widgets/framework.dart:4535:14)
  I/flutter (22248): #22     Element.updateChild 
  (package:flutter/src/widgets/framework.dart:2677:15)
  I/flutter (22248): #23     ComponentElement.performRebuild 
  (package:flutter/src/widgets/framework.dart:3557:16)
  I/flutter (22248): #24     Element.rebuild 
  (package:flutter/src/widgets/framework.dart:3446:5)
  I/flutter (22248): #25     StatefulElement.update 
  (package:flutter/src/widgets/framework.dart:3682:5)
  I/flutter (22248): #26     Element.updateChild 
  (package:flutter/src/widgets/framework.dart:2677:15)
  I/flutter (22248): #27     SingleChildRenderObjectElement.update 
  (package:flutter/src/widgets/framework.dart:4535:14)
  I/flutter (22248): #28     Element.updateChild 
  (package:flutter/src/widgets/framework.dart:2677:15)
  I/flutter (22248): #29     SingleChildRenderObjectElement.update 
  (package:flutter/src/widgets/framework.dart:4535:14)
  I/flutter (22248): #30     Element.updateChild 
  (package:flutter/src/widgets/framework.dart:2677:15)
  I/flutter (22248): #31     ComponentElement.performRebuild 
  (package:flutter/src/widgets/framework.dart:3557:16)
  I/flutter (22248): #32     Element.rebuild 
  (package:flutter/src/widgets/framework.dart:3446:5)
  I/flutter (22248): #33     StatefulElement.update 
  (package:flutter/src/widgets/framework.dart:3682:5)
  I/flutter (22248): #34     Element.updateChild 
  (package:flutter/src/widgets/framework.dart:2677:15)
  I/flutter (22248): #35     ComponentElement.performRebuild 
  (package:flutter/src/widgets/framework.dart:3557:16)
  I/flutter (22248): #36     Element.rebuild 
  (package:flutter/src/widgets/framework.dart:3446:5)
  I/flutter (22248): #37     ProxyElement.update 
  (package:flutter/src/widgets/framework.dart:3792:5)
  I/flutter (22248): #38     Element.updateChild 
  (package:flutter/src/widgets/framework.dart:2677:15)
  I/flutter (22248): #39     SingleChildRenderObjectElement.update 
  (package:flutter/src/widgets/framework.dart:4535:14)
  I/flutter (22248): #40     Element.updateChild 
  (package:flutter/src/widgets/framework.dart:2677:15)
  I/flutter (22248): #41     ComponentElement.performRebuild 
  (package:flutter/src/widgets/framework.dart:3557:16)
  I/flutter (22248): #42     Element.rebuild 
  (package:flutter/src/widgets/framework.dart:3446:5)
  I/flutter (22248): #43     StatelessElement.update 
  (package:flutter/src/widgets/framework.dart:3607:5)
  I/flutter (22248): #44     Element.updateChild 
  (package:flutter/src/widgets/framework.dart:2677:15)
  I/flutter (22248): #45     ComponentElement.performRebuild 
  (package:flutter/src/widgets/framework.dart:3557:16)
  I/flutter (22248): #46     Element.rebuild 
  (package:flutter/src/widgets/framework.dart:3446:5)
  I/flutter (22248): #47     StatelessElement.update 
  (package:flutter/src/widgets/framework.dart:3607:5)
  I/flutter (22248): #48     Element.updateChild 
  (package:flutter/src/widgets/framework.dart:2677:15)
  I/flutter (22248): #49     ComponentElement.performRebuild  
  (package:flutter/src/widgets/framework.dart:3557:16)
   I/flutter (22248): #50     Element.rebuild 
   (package:flutter/src/widgets/framework.dart:3446:5)
  I/flutter (22248): #51     StatelessElement.update 
  (package:flutter/src/widgets/framework.dart:3607:5)
  I/flutter (22248): #52     Element.updateChild 
  (package:flutter/src/widgets/framework.dart:2677:15)
   I/flutter (22248): #53     ComponentElement.performRebuild 
  (package:flutter/src/widgets/framework.dart:3557:16)
   I/flutter (22248): #54     Element.rebuild 
  (package:flutter/src/widgets/framework.dart:3446:5)
  I/flutter (22248): #55     StatelessElement.update 
  (package:flutter/src/widgets/framework.dart:3607:5)
  I/flutter (22248): #56     Element.updateChild 
  (package:flutter/src/widgets/framework.dart:2677:15)
  I/flutter (22248): #57     ComponentElement.performRebuild                                              
   (package:flutter/src/widgets/framework.dart:3557:16)
   I/flutter (22248): #58     Element.rebuild 
   (package:flutter/src/widgets/framework.dart:3446:5)
  I/flutter (22248): #59     ProxyElement.update 
  (package:flutter/src/widgets/framework.dart:3792:5)
   I/flutter (22248): #60     Element.updateChild 
   (package:flutter/src/widgets/framework.dart:2677:15)
    I/flutter (22248): #61     ComponentElement.performRebuild 
   (package:flutter/src/widgets/framework.dart:3557:16)
   I/flutter (22248): #62     Element.rebuild 
   (package:flutter/src/widgets/framework.dart:3446:5)
   I/flutter (22248): #63     ProxyElement.update 
   (package:flutter/src/widgets/framework.dart:3792:5)
    I/flutter (22248): #64     Element.updateChild 
    (package:flutter/src/widgets/framework.dart:2677:15)
    I/flutter (22248): #65     ComponentElement.performRebuild 
    (package:flutter/src/widgets/framework.dart:3557:16)
            I/flutter (22248): #66     Element.rebuild 
     (package:flutter/src/widgets/framework.dart:3446:5)
    I/flutter (22248): #67     StatefulElement.update 
    (package:flutter/src/widgets/framework.dart:3682:5)
    I/flutter (22248): #68     Element.updateChild 
      (package:flutter/src/widgets/framework.dart:2677:15)
    I/flutter (22248): #69     ComponentElement.performRebuild 
    (package:flutter/src/widgets/framework.dart:3557:16)
      I/flutter (22248): #70     Element.rebuild 
     (package:flutter/src/widgets/framework.dart:3446:5)
      I/flutter (22248): #71     ProxyElement.update 
    (package:flutter/src/widgets/framework.dart:3792:5)
    I/flutter (22248): #72     Element.updateChild 
    (package:flutter/src/widgets/framework.dart:2677:15)
     I/flutter (22248): #73     ComponentElement.performRebuild 
    (package:flutter/src/widgets/framework.dart:3557:16)
    I/flutter (22248): #74     Element.rebuild 
    (package:flutter/src/widgets/framework.dart:3446:5)
    I/flutter (22248): #75     StatefulElement.update 
   (package:flutter/src/widgets/framework.dart:3682:5)
    I/flutter (22248): #76     Element.updateChild 
   (package:flutter/src/widgets/framework.dart:2677:15)
   I/flutter (22248): #77     ComponentElement.performRebuild 
    (package:flutter/src/widgets/framework.dart:3557:16)
    I/flutter (22248): #78     Element.rebuild 
    (package:flutter/src/widgets/framework.dart:3446:5)
     I/flutter (22248): #79     ProxyElement.update 
    (package:flutter/src/widgets/framework.dart:3792:5)
     I/flutter (22248): #80     Element.updateChild 
      (package:flutter/src/widgets/framework.dart:2677:15)
   I/flutter (22248): #81     ComponentElement.performRebuild 
       (package:flutter/src/widgets/framework.dart:3557:16)
      I/flutter (22248): #82     Element.rebuild 
       (package:flutter/src/widgets/framework.dart:3446:5)
        I/flutter (22248): #83     ProxyElement.update 
       (package:flutter/src/widgets/framework.dart:3792:5)
     I/flutter (22248): #84     Element.updateChild 
      (package:flutter/src/widgets/framework.dart:2677:15)
     I/flutter (22248): #85     ComponentElement.performRebuild 
      (package:flutter/src/widgets/framework.dart:3557:16)
         I/flutter (22248): #86     Element.rebuild 
   (package:flutter/src/widgets/framework.dart:3446:5)
    I/flutter (22248): #87     StatelessElement.update 
       (package:flutter/src/widgets/framework.dart:3607:5)
     I/flutter (22248): #88     Element.updateChild 
    (package:flutter/src/widgets/framework.dart:2677:15)
       I/flutter (22248): #89     ComponentElement.performRebuild 
    (package:flutter/src/widgets/framework.dart:3557:16)
     I/flutter (22248): #90     Element.rebuild 
    (package:flutter/src/widgets/framework.dart:3446:5)
       I/flutter (22248): #91     StatefulElement.update 
    (package:flutter/src/widgets/framework.dart:3682:5)
    I/flutter (22248): #92     Element.updateChild 
    (package:flutter/src/widgets/framework.dart:2677:15)
    I/flutter (22248): #93     ComponentElement.performRebuild 
    (package:flutter/src/widgets/framework.dart:3557:16)
     I/flutter (22248): #94     Element.rebuild 
    (package:flutter/src/widgets/framework.dart:3446:5)
    I/flutter (22248): #95     ProxyElement.update 
      (package:flutter/src/widgets/framework.dart:3792:5)
     I/flutter (22248): #96     Element.updateChild 
     (package:flutter/src/widgets/framework.dart:2677:15)
     I/flutter (22248): #97     ComponentElement.performRebuild 
     (package:flutter/src/widgets/framework.dart:3557:16)
      I/flutter (22248): #98     Element.rebuild 
     (package:flutter/src/widgets/framework.dart:3446:5)
      I/flutter (22248): #99     BuildOwner.buildScope 
      (package:flutter/src/widgets/framework.dart:2246:33)
        I/flutter (22248): #100    
  
  
  
  
  
  
  
  
  
  
  
  
        BindingBase&amp;SchedulerBinding&amp;GestureBinding&amp;ServicesBinding&amp;Renderer
       Binding&amp;WidgetsBinding.drawFrame 
        (package:flutter/src/widgets/binding.dart:580:20)
         I/flutter (22248): #101    
  
  
  
  
  
      BindingBase&amp;SchedulerBinding&amp;GestureBinding&amp;ServicesBinding&amp;Renderer
    Binding._handlePersistentFrameCallback 
     (package:flutter/src/rendering/binding.dart:200:5)
      I/flutter (22248): #102    
      BindingBase&amp;SchedulerBinding._invokeFrameCallback 
     (package:flutter/src/scheduler/binding.dart:713:15)
     I/flutter (22248): #103    BindingBase&amp;SchedulerBinding.handleDrawFrame 
       (package:flutter/src/scheduler/binding.dart:649:9)
       I/flutter (22248): #104    
  
  
  
  
     BindingBase&amp;SchedulerBinding&amp;GestureBinding&amp;ServicesBinding&amp;Renderer
     Binding.scheduleWarmUpFrame.&lt;anonymous closure&gt; 
      (package:flutter/src/rendering/binding.dart:286:20)
     I/flutter (22248): #106    _Timer._runTimers (dart:isolate-
        patch/dart:isolate/timer_impl.dart:367)
        I/flutter (22248): #107    _Timer._handleMessage (dart:isolate-
       patch/dart:isolate/timer_impl.dart:401)
       I/flutter (22248): #108    _RawReceivePortImpl._handleMessage 
         (dart:isolate-patch/dart:isolate/isolate_patch.dart:163)
           I/flutter (22248): (elided 3 frames from class _AssertionError and 
         package dart:async-patch)
         I/flutter (22248): 
  
    ══════════════════════════════════════════════════════════════════════════
    ══════════════════════════
    Reloaded 0 of 492 libraries in 823ms.
  </code></pre>
  <p>I have included a sample example of what i am trying to do in my application.</p>
  <p>main.dart</p>
  <pre><code>import 'package:flutter/rendering.dart';
  import 'package:flutter/material.dart';
  import 'package:untitled3/secondwidget.dart';
  
  void main() {
    runApp(
      new Example(),
    );
  }
  
  class Example extends StatefulWidget {
    @override
    _ExampleState createState() =&gt; new _ExampleState();
  }
  
  class _ExampleState extends State&lt;Example&gt; {
    int noOfBranches;
  
    @override
    void initState() {
      super.initState();
      getCount();
    }
  
    void getCount() {
      print('list builder count is ${noOfBranches}');
    }
  
    @override
    Widget build(BuildContext context) {
      setState(() {
        noOfBranches = 2;
      });
  
      return new MaterialApp(
        home: new Scaffold(
          body: new ListView.builder(
          itemCount: noOfBranches,
          itemBuilder: (BuildContext context, int index) {
            return new Card(
              child: new FlatButton(
                  onPressed: (() {
                    Navigator.push(
                        context,
                        new MaterialPageRoute(
                            builder: (_) =&gt; new SecondWidget()));
                  }),
                  child: new Text('Next Widget')),
            );
          }),
        ),
      );
    }
   }
  </code></pre>
  <p>secondwidget.dart</p>
  <pre><code>import 'package:flutter/material.dart';
  import 'package:flutter/rendering.dart';
  
    class SecondWidget extends StatefulWidget {
    @override
   _SecondWidgetState createState() =&gt; new _SecondWidgetState();
   }
  
   class _SecondWidgetState extends State&lt;SecondWidget&gt; {
    @override
    Widget build(BuildContext context) {
      return new Container(
        child: new Text('Second Widget'),
     );
    }
   }   
  </code></pre>
  <p>I don't see the error in my application if <code>setState()</code> declare as given in the example main.dart file inside the</p>
  <pre><code>@override
     Widget build(BuildContext context) {
       setState(() {
      noOfBranches = 2;
        });
        .....
        }
   
  </code></pre>
  <p>but I only see this error if I declare the <code>setState()</code> inside the <code>getCount()</code> function.</p>
  <p>I am not so sure why this is the case.</p>
  <p>I am actually stuck on this error for two days and any help would be greatly appreciated.</p>
  <p>Many Thanks,
  Mahi</p>