Reprint please indicate the source: juejin.cn/post/684490… This post is from: Wos homepage

In the event of'_debugUltimatePreviousSiblingOf(after, equals: _firstChild)': is not true.“, check whether the error is caused by the following reasons, and modify the error according to the following

Note: this article is a very old version and may not be applicable to a new version. For reference only, use with caution

Example:

class _HomeContainerState extends State<HomeContainer> {
  PageController _pageController;
  int _currentIndex = 0;

  @override
  void initState() {
    super.initState();
    _pageController = new PageController();
  }
  
  @override
  void dispose() {
    _pageController.dispose();
    super.dispose();
  }

  @override
  Widget build(BuildContext context) {
    return new Scaffold(
      body: PageView(
        children: <Widget>[
          new Container(
            child: new Page1(),
          ),
          new Container(
            child: new Page2(),
          ),
          new Container(
            child: new Page3(),
          ),
          Container(
            child: new Page4(),
          ),
        ],
        controller: _pageController,
        physics: NeverScrollableScrollPhysics(),
        onPageChanged: (int index){
          setState(() {
            _currentIndex = index;
          });
        },
      ),
      bottomNavigationBar: BottomNavigationBar(
        items: [
          _buildItem('page1'),
          _buildItem('page2'),
          _buildItem('page3'),
          _buildItem('page4'),
        ],
        onTap: (int index) {
          _pageController.jumpToPage(index);
        },
        currentIndex: _currentIndex,
        type: BottomNavigationBarType.fixed,
      ),
    );
  }

  _buildItem(String title){
    returnBottomNavigationBarItem( icon: Icon( Icons.account_balance, ), title: Text(title), ); }}Copy the code


The above code will cause a crash with the following error message:

Error message:

Restarted app in2823 ms. I/flutter (24543) : ═ ═ ╡ EXCEPTION CAUGHT BY WIDGETS LIBRARY ╞ ═ ═ ═ ═ ═ ═ ═ ═ ═ ═ ═ ═ ═ ═ ═ ═ ═ ═ ═ ═ ═ ═ ═ ═ ═ ═ ═ ═ ═ ═ ═ ═ ═ ═ ═ ═ ═ ═ ═ ═ ═ ═ ═ ═ ═ ═ ═ ═ ═ ═ ═ ═ ═ ═ ═ ═ ═ ═ ═ I/flutter (24543) : The following assertion was thrown building NotificationListener<KeepAliveNotification>: I/flutter (24543):'package:flutter/src/rendering/object.dart': Failed assertion: line 2775 pos 14:
I/flutter (24543): '_debugUltimatePreviousSiblingOf(after, equals: _firstChild)': is not true.
I/flutter (24543): Either the assertion indicates an error in the framework itself, or we should provide substantially
I/flutter (24543): more information in this error message to help you determine and fix the underlying cause.
I/flutter (24543): In either case, please report this assertion by filing a bug on GitHub:
I/flutter (24543):   https://github.com/flutter/flutter/issues/new
I/flutter (24543): When the exception was thrown, this was the stack:
I/flutter (24543): #2 _RenderSliverMultiBoxAdaptor&RenderSliver&ContainerRenderObjectMixin._insertIntoChildList (package:flutter/src/rendering/object.dart)
I/flutter (24543): #3 _RenderSliverMultiBoxAdaptor&RenderSliver&ContainerRenderObjectMixin.insert (package:flutter/src/rendering/object.dart:2809:5)
I/flutter (24543): #4 RenderSliverMultiBoxAdaptor.insert (package:flutter/src/rendering/sliver_multi_box_adaptor.dart:209:11)
I/flutter (24543): #5 SliverMultiBoxAdaptorElement.insertChildRenderObject (package:flutter/src/widgets/sliver.dart:865:18)
I/flutter (24543): #6 RenderObjectElement.attachRenderObject (package:flutter/src/widgets/framework.dart:4513:35)
I/flutter (24543): #7 RenderObjectElement.mount (package:flutter/src/widgets/framework.dart:4279:5)
I/flutter (24543): #8 SingleChildRenderObjectElement.mount (package:flutter/src/widgets/framework.dart:4653:11)
I/flutter (24543): #9 Element.inflateWidget (package:flutter/src/widgets/framework.dart:2907:14)
I/flutter (24543): #10 Element.updateChild (package:flutter/src/widgets/framework.dart:2710:12)
I/flutter (24543): #11 ComponentElement.performRebuild (package:flutter/src/widgets/framework.dart:3653:16)
I/flutter (24543): #12 Element.rebuild (package:flutter/src/widgets/framework.dart:3495:5)
I/flutter (24543): #13 ComponentElement._firstBuild (package:flutter/src/widgets/framework.dart:3622:5)
I/flutter (24543): #14 ComponentElement.mount (package:flutter/src/widgets/framework.dart:3617:5)
I/flutter (24543): #15 Element.inflateWidget (package:flutter/src/widgets/framework.dart:2907:14)
I/flutter (24543): #16 Element.updateChild (package:flutter/src/widgets/framework.dart:2710:12)
I/flutter (24543): #17 ComponentElement.performRebuild (package:flutter/src/widgets/framework.dart:3653:16)
I/flutter (24543): #18 Element.rebuild (package:flutter/src/widgets/framework.dart:3495:5)
I/flutter (24543): #19 ComponentElement._firstBuild (package:flutter/src/widgets/framework.dart:3622:5)
I/flutter (24543): #20 ComponentElement.mount (package:flutter/src/widgets/framework.dart:3617:5)
I/flutter (24543): #21 ParentDataElement.mount (package:flutter/src/widgets/framework.dart:3955:11)
I/flutter (24543): #22 Element.inflateWidget (package:flutter/src/widgets/framework.dart:2907:14)
I/flutter (24543): #23 Element.updateChild (package:flutter/src/widgets/framework.dart:2710:12)
I/flutter (24543): #24 ComponentElement.performRebuild (package:flutter/src/widgets/framework.dart:3653:16)
I/flutter (24543): #25 Element.rebuild (package:flutter/src/widgets/framework.dart:3495:5)
I/flutter (24543): #26 ComponentElement._firstBuild (package:flutter/src/widgets/framework.dart:3622:5)
I/flutter (24543): #27 StatefulElement._firstBuild (package:flutter/src/widgets/framework.dart:3769:11)
I/flutter (24543): #28 ComponentElement.mount (package:flutter/src/widgets/framework.dart:3617:5)
I/flutter (24543): #29 Element.inflateWidget (package:flutter/src/widgets/framework.dart:2907:14)
I/flutter (24543): #30 Element.updateChild (package:flutter/src/widgets/framework.dart:2710:12)
I/flutter (24543): #31 SliverMultiBoxAdaptorElement.updateChild (package:flutter/src/widgets/sliver.dart:744:36)
I/flutter (24543): #32 SliverMultiBoxAdaptorElement.performRebuild (package:flutter/src/widgets/sliver.dart:702:34)
I/flutter (24543): #33 SliverMultiBoxAdaptorElement.update (package:flutter/src/widgets/sliver.dart:671:7)
I/flutter (24543): #34 Element.updateChild (package:flutter/src/widgets/framework.dart:2699:15)
I/flutter (24543): #35 RenderObjectElement.updateChildren (package:flutter/src/widgets/framework.dart:4379:32)
I/flutter (24543): #36 MultiChildRenderObjectElement.update (package:flutter/src/widgets/framework.dart:4769:17)
I/flutter (24543): #37 _ViewportElement.update (package:flutter/src/widgets/viewport.dart:192:11)
I/flutter (24543): #38 Element.updateChild (package:flutter/src/widgets/framework.dart:2699:15)
I/flutter (24543): #39 ComponentElement.performRebuild (package:flutter/src/widgets/framework.dart:3653:16)
I/flutter (24543): #40 Element.rebuild (package:flutter/src/widgets/framework.dart:3495:5)
I/flutter (24543): #41 ProxyElement.update (package:flutter/src/widgets/framework.dart:3909:5)
I/flutter (24543): #42 Element.updateChild (package:flutter/src/widgets/framework.dart:2699:15)
I/flutter (24543): #43 SingleChildRenderObjectElement.update (package:flutter/src/widgets/framework.dart:4661:14)
I/flutter (24543): #44 Element.updateChild (package:flutter/src/widgets/framework.dart:2699:15)
I/flutter (24543): #45 SingleChildRenderObjectElement.update (package:flutter/src/widgets/framework.dart:4661:14)
I/flutter (24543): #46 Element.updateChild (package:flutter/src/widgets/framework.dart:2699:15)
I/flutter (24543): #47 SingleChildRenderObjectElement.update (package:flutter/src/widgets/framework.dart:4661:14)
I/flutter (24543): #48 Element.updateChild (package:flutter/src/widgets/framework.dart:2699:15)
I/flutter (24543): #49 SingleChildRenderObjectElement.update (package:flutter/src/widgets/framework.dart:4661:14)
I/flutter (24543): #50 Element.updateChild (package:flutter/src/widgets/framework.dart:2699:15)
I/flutter (24543): #51 ComponentElement.performRebuild (package:flutter/src/widgets/framework.dart:3653:16)
I/flutter (24543): #52 Element.rebuild (package:flutter/src/widgets/framework.dart:3495:5)
I/flutter (24543): #53 StatefulElement.update (package:flutter/src/widgets/framework.dart:3799:5)
I/flutter (24543): #54 Element.updateChild (package:flutter/src/widgets/framework.dart:2699:15)
I/flutter (24543): #55 SingleChildRenderObjectElement.update (package:flutter/src/widgets/framework.dart:4661:14)
I/flutter (24543): #56 Element.updateChild (package:flutter/src/widgets/framework.dart:2699:15)
I/flutter (24543): #57 SingleChildRenderObjectElement.update (package:flutter/src/widgets/framework.dart:4661:14)
I/flutter (24543): #58 Element.updateChild (package:flutter/src/widgets/framework.dart:2699:15)
I/flutter (24543): #59 SingleChildRenderObjectElement.update (package:flutter/src/widgets/framework.dart:4661:14)
I/flutter (24543): #60 Element.updateChild (package:flutter/src/widgets/framework.dart:2699:15)
I/flutter (24543): #61 SingleChildRenderObjectElement.update (package:flutter/src/widgets/framework.dart:4661:14)
I/flutter (24543): #62 Element.updateChild (package:flutter/src/widgets/framework.dart:2699:15)
I/flutter (24543): #63 ComponentElement.performRebuild (package:flutter/src/widgets/framework.dart:3653:16)
I/flutter (24543): #64 Element.rebuild (package:flutter/src/widgets/framework.dart:3495:5)
I/flutter (24543): #65 StatelessElement.update (package:flutter/src/widgets/framework.dart:3702:5)
I/flutter (24543): #66 Element.updateChild (package:flutter/src/widgets/framework.dart:2699:15)
I/flutter (24543): #67 ComponentElement.performRebuild (package:flutter/src/widgets/framework.dart:3653:16)
I/flutter (24543): #68 Element.rebuild (package:flutter/src/widgets/framework.dart:3495:5)
I/flutter (24543): #69 StatefulElement.update (package:flutter/src/widgets/framework.dart:3799:5)
I/flutter (24543): #70 Element.updateChild (package:flutter/src/widgets/framework.dart:2699:15)
I/flutter (24543): #71 ComponentElement.performRebuild (package:flutter/src/widgets/framework.dart:3653:16)
I/flutter (24543): #72 Element.rebuild (package:flutter/src/widgets/framework.dart:3495:5)
I/flutter (24543): #73 StatefulElement.update (package:flutter/src/widgets/framework.dart:3799:5)
I/flutter (24543): #74 Element.updateChild (package:flutter/src/widgets/framework.dart:2699:15)
I/flutter (24543): #75 ComponentElement.performRebuild (package:flutter/src/widgets/framework.dart:3653:16)
I/flutter (24543): #76 Element.rebuild (package:flutter/src/widgets/framework.dart:3495:5)
I/flutter (24543): #77 StatelessElement.update (package:flutter/src/widgets/framework.dart:3702:5)
I/flutter (24543): #78 Element.updateChild (package:flutter/src/widgets/framework.dart:2699:15)
I/flutter (24543): #79 ComponentElement.performRebuild (package:flutter/src/widgets/framework.dart:3653:16)
I/flutter (24543): #80 Element.rebuild (package:flutter/src/widgets/framework.dart:3495:5)
I/flutter (24543): #81 StatefulElement.update (package:flutter/src/widgets/framework.dart:3799:5)
I/flutter (24543): #82 Element.updateChild (package:flutter/src/widgets/framework.dart:2699:15)
I/flutter (24543): #83 ComponentElement.performRebuild (package:flutter/src/widgets/framework.dart:3653:16)
I/flutter (24543): #84 Element.rebuild (package:flutter/src/widgets/framework.dart:3495:5)
I/flutter (24543): #85 ProxyElement.update (package:flutter/src/widgets/framework.dart:3909:5)
I/flutter (24543): #86 Element.updateChild (package:flutter/src/widgets/framework.dart:2699:15)
I/flutter (24543): #87 ComponentElement.performRebuild (package:flutter/src/widgets/framework.dart:3653:16)
I/flutter (24543): #88 Element.rebuild (package:flutter/src/widgets/framework.dart:3495:5)
I/flutter (24543): #89 ProxyElement.update (package:flutter/src/widgets/framework.dart:3909:5)
I/flutter (24543): #90 Element.updateChild (package:flutter/src/widgets/framework.dart:2699:15)
I/flutter (24543): #91 RenderObjectElement.updateChildren (package:flutter/src/widgets/framework.dart:4379:32)
I/flutter (24543): #92 MultiChildRenderObjectElement.update (package:flutter/src/widgets/framework.dart:4769:17)
I/flutter (24543): #93 Element.updateChild (package:flutter/src/widgets/framework.dart:2699:15)
I/flutter (24543): #94 ComponentElement.performRebuild (package:flutter/src/widgets/framework.dart:3653:16)
I/flutter (24543): #95 Element.rebuild (package:flutter/src/widgets/framework.dart:3495:5)
I/flutter (24543): #96 StatefulElement.update (package:flutter/src/widgets/framework.dart:3799:5)
I/flutter (24543): #97 Element.updateChild (package:flutter/src/widgets/framework.dart:2699:15)
I/flutter (24543): #98 ComponentElement.performRebuild (package:flutter/src/widgets/framework.dart:3653:16)
I/flutter (24543): #99 Element.rebuild (package:flutter/src/widgets/framework.dart:3495:5)
I/flutter (24543): #100 ProxyElement.update (package:flutter/src/widgets/framework.dart:3909:5)
I/flutter (24543): #101 Element.updateChild (package:flutter/src/widgets/framework.dart:2699:15)
I/flutter (24543): #102 ComponentElement.performRebuild (package:flutter/src/widgets/framework.dart:3653:16)
I/flutter (24543): #103 Element.rebuild (package:flutter/src/widgets/framework.dart:3495:5)
I/flutter (24543): #104 StatefulElement.update (package:flutter/src/widgets/framework.dart:3799:5)
I/flutter (24543): #105 Element.updateChild (package:flutter/src/widgets/framework.dart:2699:15)
I/flutter (24543): #106 SingleChildRenderObjectElement.update (package:flutter/src/widgets/framework.dart:4661:14)
I/flutter (24543): #107 Element.updateChild (package:flutter/src/widgets/framework.dart:2699:15)
I/flutter (24543): #108 ComponentElement.performRebuild (package:flutter/src/widgets/framework.dart:3653:16)
I/flutter (24543): #109 Element.rebuild (package:flutter/src/widgets/framework.dart:3495:5)
I/flutter (24543): #110 StatelessElement.update (package:flutter/src/widgets/framework.dart:3702:5)
I/flutter (24543): #111 Element.updateChild (package:flutter/src/widgets/framework.dart:2699:15)
I/flutter (24543): #112 SingleChildRenderObjectElement.update (package:flutter/src/widgets/framework.dart:4661:14)
I/flutter (24543): #113 Element.updateChild (package:flutter/src/widgets/framework.dart:2699:15)
I/flutter (24543): #114 ComponentElement.performRebuild (package:flutter/src/widgets/framework.dart:3653:16)
I/flutter (24543): #115 Element.rebuild (package:flutter/src/widgets/framework.dart:3495:5)
I/flutter (24543): #116 StatefulElement.update (package:flutter/src/widgets/framework.dart:3799:5)
I/flutter (24543): #117 Element.updateChild (package:flutter/src/widgets/framework.dart:2699:15)
I/flutter (24543): #118 ComponentElement.performRebuild (package:flutter/src/widgets/framework.dart:3653:16)
I/flutter (24543): #119 Element.rebuild (package:flutter/src/widgets/framework.dart:3495:5)
I/flutter (24543): #120 StatefulElement.update (package:flutter/src/widgets/framework.dart:3799:5)
I/flutter (24543): #121 Element.updateChild (package:flutter/src/widgets/framework.dart:2699:15)
I/flutter (24543): #122 ComponentElement.performRebuild (package:flutter/src/widgets/framework.dart:3653:16)
I/flutter (24543): #123 Element.rebuild (package:flutter/src/widgets/framework.dart:3495:5)
I/flutter (24543): #124 ProxyElement.update (package:flutter/src/widgets/framework.dart:3909:5)
I/flutter (24543): #125 Element.updateChild (package:flutter/src/widgets/framework.dart:2699:15)
I/flutter (24543): #126 ComponentElement.performRebuild (package:flutter/src/widgets/framework.dart:3653:16)
I/flutter (24543): #127 Element.rebuild (package:flutter/src/widgets/framework.dart:3495:5)
I/flutter (24543): #128 ProxyElement.update (package:flutter/src/widgets/framework.dart:3909:5)
I/flutter (24543): #129 Element.updateChild (package:flutter/src/widgets/framework.dart:2699:15)
I/flutter (24543): #130 ComponentElement.performRebuild (package:flutter/src/widgets/framework.dart:3653:16)
I/flutter (24543): #131 Element.rebuild (package:flutter/src/widgets/framework.dart:3495:5)
I/flutter (24543): #132 StatefulElement.update (package:flutter/src/widgets/framework.dart:3799:5)
I/flutter (24543): #133 Element.updateChild (package:flutter/src/widgets/framework.dart:2699:15)
I/flutter (24543): #134 ComponentElement.performRebuild (package:flutter/src/widgets/framework.dart:3653:16)
I/flutter (24543): #135 Element.rebuild (package:flutter/src/widgets/framework.dart:3495:5)
I/flutter (24543): #136 BuildOwner.buildScope (package:flutter/src/widgets/framework.dart:2242:33)
I/flutter (24543): # 137 _WidgetsFlutterBinding&BindingBase&GestureBinding&ServicesBinding&SchedulerBinding&PaintingBinding&RendererBinding&Widge tsBinding.drawFrame (package:flutter/src/widgets/binding.dart:626:20)
I/flutter (24543): # 138 _WidgetsFlutterBinding&BindingBase&GestureBinding&ServicesBinding&SchedulerBinding&PaintingBinding&RendererBinding._hand lePersistentFrameCallback (package:flutter/src/rendering/binding.dart:208:5)
I/flutter (24543): #139 _WidgetsFlutterBinding&BindingBase&GestureBinding&ServicesBinding&SchedulerBinding._invokeFrameCallback (package:flutter/src/scheduler/binding.dart:990:15)
I/flutter (24543): #140 _WidgetsFlutterBinding&BindingBase&GestureBinding&ServicesBinding&SchedulerBinding.handleDrawFrame (package:flutter/src/scheduler/binding.dart:930:9)
I/flutter (24543): #141 _WidgetsFlutterBinding&BindingBase&GestureBinding&ServicesBinding&SchedulerBinding._handleDrawFrame (package:flutter/src/scheduler/binding.dart:842:5)
I/flutter (24543): #142 _invoke (dart:ui/hooks.dart:120:13)
I/flutter (24543): #143 _drawFrame (dart:ui/hooks.dart:109:3)
I/flutter (24543): (elided 2 frames from class _AssertionError)
Copy the code


Here are two simple solutions:

Solution 1:

Replace jumpToPage with animateToPage

_pageController.animateToPage(
  index,
  duration: Duration(milliseconds: 260),
  curve: Curves.fastOutSlowIn,
)
Copy the code

Simple, but animated.

Solution 2:

Delete onPageChanged, through PageController addListener listening page changes

_pageController = new PageController() .. addListener(() {if(_currentIndex ! = _pageController.page.round()) {setState(() { _currentIndex = _pageController.page.round(); }); }});Copy the code

This method only in the disable PageView sliding operation (NeverScrollableScrollPhysics) and use _pageController. JumpToPage to switch from the page.

Solution 3:

Use IndexedStack instead of PageView

Solution 4: (Recommended)

  1. Create a new file: rebuild_layout.dart

  2. Copy the following code into the file

import 'package:flutter/material.dart';

class RebuildLayout extends StatefulWidget {
  final WidgetBuilder builder;
  final RebuildLayoutController controller;

  RebuildLayout({Key key, @required this.builder, @required this.controller}) : super(key: key);

  @override
  _RebuildLayoutState createState() => _RebuildLayoutState();
}

class _RebuildLayoutState extends State<RebuildLayout> {
  @override
  void initState() {
    super.initState();

    widget.controller.addListener(_onRebuild);
  }

  @override
  void didUpdateWidget(RebuildLayout oldWidget) {
    super.didUpdateWidget(oldWidget);

    if(oldWidget.controller ! = widget.controller) { oldWidget.controller.removeListener(_onRebuild); widget.controller.addListener(_onRebuild); } } @override voiddispose() {
    widget.controller._listeners.clear();
    super.dispose();
  }

  _onRebuild() => setState(() {});

  @override
  Widget build(BuildContext context) {
    return widget.builder(context);
  }
}

class RebuildLayoutController extends Listenable {
  List<VoidCallback> _listeners = [];

  @override
  void addListener(listener) {
    _listeners.add(listener);
  }

  @override
  void removeListener(listener) {
    _listeners.remove(listener);
  }

  notification() {
    for(var listener in_listeners){ listener(); }}}Copy the code
  1. inBottomNavigationBarPut a layer on the outsideRebuildLayout
bottomNavigationBar: RebuildLayout(builder: (BuildContext context){
  return BottomNavigationBar(
    ...
    onTap: (int index) {
      _pageController.jumpToPage(index);
    },
    currentIndex: _currentIndex,
    type: BottomNavigationBarType.fixed,
  );
}, controller: _rebuildLayoutController),
Copy the code
  1. ininitState()Declaration cycle created in_rebuildLayoutController
RebuildLayoutController _rebuildLayoutController;

@override
void initState() { super.initState(); . _rebuildLayoutController = RebuildLayoutController(); }Copy the code
  1. inPageViewtheonPageChangedProperty.rebuildLayoutController.notification()
body: PageView( ... onPageChanged: (int index){ _currentIndex = index; rebuildLayoutController.notification(); },),Copy the code

The idea behind RebuildLayout is that it only informs its children of the rebuild and does not affect other controls.

Before RebuildLayout is used, setState is called in the onPageChanged callback to refresh the page, which causes both PageView and BottomNavigationBar to be rebuilt, resulting in performance loss.

This control is almost completely decoupled, so it can be used in many places where only partial refreshes are desired.


Above, hope to solve your problem 🙂