dart 错误:“_elements.contains(元素)”:不是真的

cnwbcb6i  于 2023-03-10  发布在  其他
关注(0)|答案(2)|浏览(118)

我看到这个错误'_elements.contains(element)': is not true.在过去两天在我的flutter项目,这是工作绝对良好,因为一开始.我很抱歉地说,但我不知道这个错误,但我认为我错过了什么是导致这个错误.此外,当我热重新加载相同的页面布局填充,但抛出异常.
这也间歇性地显示检测到重复的GlobalKeys(),但一开始不是这样。
如果以前有人偶然发现了这些错误,请帮助我。我真的不能写一个例子来重现同样的问题。非常感谢。
我已经给出了错误的详细信息如下:

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&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.

我已经包括了一个样本的例子,我试图做什么,在我的应用程序。
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'),
   );
  }
 }

如果setState()声明为示例main.dart文件中给定的值,则在应用程序中看不到错误

@override
   Widget build(BuildContext context) {
     setState(() {
    noOfBranches = 2;
      });
      .....
      }

但是只有在getCount()函数中声明setState()时,我才会看到这个错误。
我不太清楚为什么会这样。
我实际上被这个错误困了两天,任何帮助都将不胜感激。
非常感谢,Mahi

rkue9o1l

rkue9o1l1#

我想我通过在

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

现在这个应用程序运行得非常好,没有任何问题。但是,如果有更多的建议,我将不胜感激,因为我是编程新手,我会非常非常高兴地了解更多。唯一的问题是setState()正在修改上下文,当点击按钮调用另一个小部件时,它会弹出这个错误消息。
非常感谢,Mahi。

cbeh67ev

cbeh67ev2#

为什么不在initState方法中设置noOfBranches变量呢?

@override
void initState() {
  super.initState();
  noOfBranches = 2;
}

相关问题