Error: '_elements.contains(element)': is not true

1,757 views
Skip to first unread message

mahe...@prayuta.com

unread,
Oct 13, 2017, 8:58:42 AM10/13/17
to Flutter Dev
Hello DevTeam,

I am seeing this error ` '_elements.contains(element)': is not true.`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.

Intermittently this was also showing Duplicate GlobalKeys() detected, but this wasn't the case in the beginning.

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.

I have given the error details as follows:
 

    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): 
    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&SchedulerBinding&GestureBinding&ServicesBinding&Renderer
         Binding&WidgetsBinding.drawFrame 
          (package:flutter/src/widgets/binding.dart:580:20)
           I/flutter (22248): #101    
  
  
  
  
  
        BindingBase&SchedulerBinding&GestureBinding&ServicesBinding&Renderer
      Binding._handlePersistentFrameCallback 
       (package:flutter/src/rendering/binding.dart:200:5)
        I/flutter (22248): #102    
        BindingBase&SchedulerBinding._invokeFrameCallback 
       (package:flutter/src/scheduler/binding.dart:713:15)
       I/flutter (22248): #103    BindingBase&SchedulerBinding.handleDrawFrame 
         (package:flutter/src/scheduler/binding.dart:649:9)
         I/flutter (22248): #104    
  
  
  
  
       BindingBase&SchedulerBinding&GestureBinding&ServicesBinding&Renderer
       Binding.scheduleWarmUpFrame.<anonymous closure> 
        (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.

I would be happy if some one could tell me the scenarios where this kind of exception arises like duplicate global keys etc.,

I did declare the keys for all the StatefulWidgets but this didn't solve my problem.

I am pretty sure that this is some kind of vague question without any code but I only wanted to know the scenarios which cause this so that I can rectify my code.


Regards,
Mahi.

Ian Hickson

unread,
Oct 13, 2017, 12:27:04 PM10/13/17
to mahe...@prayuta.com, Flutter Dev
This usually indicates a problem deep in the framework. If you're able to reproduce it reliably, then it would be really fantastic if you could try to reduce your application to the smallest possible test that still reproduces the issue, and then file a bug.

--
You received this message because you are subscribed to the Google Groups "Flutter Dev" group.
To unsubscribe from this group and stop receiving emails from it, send an email to flutter-dev...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
--

--
Ian Hickson

😸

mahe...@prayuta.com

unread,
Oct 16, 2017, 9:08:59 AM10/16/17
to Flutter Dev

hi Ian, 


Thank you for your reply,


I have included a sample example of what i am trying to do in my application.

main.dart

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() => new _ExampleState();
}

class _ExampleState extends State<Example> {
  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: (_) => new SecondWidget()));
                }),
                child: new Text('Next Widget')),
          );
        }),
      ),
    );
  }
 }

secondwidget.dart

import 'package:flutter/material.dart';
import 'package:flutter/rendering.dart';

  class SecondWidget extends StatefulWidget {
  @override
 _SecondWidgetState createState() => new _SecondWidgetState();
 }

 class _SecondWidgetState extends State<SecondWidget> {
  @override
  Widget build(BuildContext context) {
    return new Container(
      child: new Text('Second Widget'),
   );
  }
 }   

But, I think I kinda solved the error by declaring the setState() inside the

    @override
  Widget build(BuildContext context) {
    setState(() {
      noOfBranches = 2;
    });
    print('noof branches inside widget build = $noOfBranches');
.....
}

Now the app is running absolutely fine without any problems. But, I would be grateful if there are any more suggestions as I am new to programming and would be very very happy to learn more. I think, the only problem was the setState() was modifying the context and when clicked on the button to call another widget it was throwing up with this error message.

Many Thanks, Mahi.

mahe...@prayuta.com

unread,
Oct 16, 2017, 9:24:27 AM10/16/17
to Flutter Dev

Hi Ian,
thank you for your reply.

I am sorry for the previous reply which was only a part of the solution.

I have posted a code for what I was doing in my application.

main.dart

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() => new _ExampleState();
}

class _ExampleState extends State<Example> {
int noOfBranches;

@override
void initState() {
super.initState();
getCount();
}

void getCount() {
    setState(() {
noOfBranches = 2;
});
    print('list builder count is ${noOfBranches}');
}

@override
Widget build(BuildContext context) {


I was actually trying to use setState() method to initialize a variable and was calling another widget using the same context.

I think I kinda solved this by calling this method inside

@override
Widget build(BuildContext context) {
setState(() {
noOfBranches = 2;
});
//widget code in here
}

My app was throwing the error if the setState() was called inside the getCount() function.
But, after declaring inside the widget there are no errors and the application is working perfectly.
The only problem I think was the setState() was modifying the current context of the state and when clicked on the button to call another widget it was throwing up with this error message.
I would be grateful if there are any more suggestions as I am new to programming and would be very  happy to learn more. 

Many Thanks,

Mahi




On Friday, October 13, 2017 at 9:57:04 PM UTC+5:30, Ian Hickson wrote:

Ian Hickson

unread,
Oct 16, 2017, 5:02:24 PM10/16/17
to mahe...@prayuta.com, Flutter Dev
Calling setState() inside of build() doesn't really make sense (setState means "call build").

I don't really understand the failure though. Can you file a bug with the code that reproduces the failure?

That would be super helpful.

Kris Giesing

unread,
Oct 16, 2017, 5:44:24 PM10/16/17
to Ian Hickson, mahe...@prayuta.com, Flutter Dev
In fact, I thought Flutter had an assert that would trip if setState() was ever called from within a build() function. If there isn't one, perhaps there should be?

To unsubscribe from this group and stop receiving emails from it, send an email to flutter-dev+unsubscribe@googlegroups.com.

For more options, visit https://groups.google.com/d/optout.
--

--
Ian Hickson

😸

--
You received this message because you are subscribed to the Google Groups "Flutter Dev" group.
To unsubscribe from this group and stop receiving emails from it, send an email to flutter-dev+unsubscribe@googlegroups.com.

mahe...@prayuta.com

unread,
Oct 18, 2017, 7:24:53 AM10/18/17
to Flutter Dev
Hi Ian, 
I am sorry for the confusion, I have now changed my code and have called the setState() outside the build. This is now working fine for me but I'm not so sure what caused the issue. I am really sorry I've tried to reproduce the problem but was unable to do it. I'll keep an eye on this one and file a bug if this happens again.

Many Thanks for your help.

Regards,
Mahi.

mahe...@prayuta.com

unread,
Oct 18, 2017, 7:26:08 AM10/18/17
to Flutter Dev
Hi Kris, 
I am sorry for the confusion, I have now changed my code and have called the setState() outside the build. This is now working fine for me but I'm not so sure what caused the issue. I am really sorry I've tried to reproduce the problem but was unable to do it. I'll keep an eye on this one and file a bug if this happens again.

Many Thanks for your help.

Regards,
Mahi.

Reply all
Reply to author
Forward
0 new messages