断言失败:第 125 行第 12 行:'assertMidButtonStyles(navBarStyle, items!.length)'

Posted

技术标签:

【中文标题】断言失败:第 125 行第 12 行:\'assertMidButtonStyles(navBarStyle, items!.length)\'【英文标题】:Failed assertion: line 125 pos 12: 'assertMidButtonStyles(navBarStyle, items!.length)'断言失败:第 125 行第 12 行:'assertMidButtonStyles(navBarStyle, items!.length)' 【发布时间】:2021-09-11 13:17:22 【问题描述】:

我在使用persistent_bottom_nav_bar时遇到了一些问题,它是flutter包。

我遇到的情况如下:

▽bottomNav.dart

import 'package:flutter/material.dart';
import 'package:persistent_bottom_nav_bar/persistent-tab-view.dart';

import 'package:project1_makers/main.dart';

class bottomNav extends StatelessWidget 
  PersistentTabController _controller = PersistentTabController(initialIndex: 0);
  
  @override
  Widget build(BuildContext context) 
    return PersistentTabView(
      context,
      controller: _controller,
      screens: _buildScreens(),
      items: _navBarsItems(),
      confineInSafeArea: true,
      backgroundColor: Colors.white,
      
      // Set Components with true/false
      handleandroidBackButtonPress: true,
      resizeToAvoidBottomInset: true,
      stateManagement: true,
      hideNavigationBarWhenKeyboardShows: true,

      decoration: NavBarDecoration(
        borderRadius: BorderRadius.circular(10.0),
        colorBehindNavBar: Colors.white,
      ),
      popAllScreensOnTapOfSelectedTab: true,
      popActionScreens: PopActionScreensType.all,
      itemAnimationProperties: ItemAnimationProperties(
        duration: Duration(milliseconds: 200),
        curve: Curves.ease,
      ),
      screenTransitionAnimation: ScreenTransitionAnimation(
        animateTabTransition: true,
        curve: Curves.ease,
        duration: Duration(milliseconds: 200),
      ),
      navBarStyle: NavBarStyle.style15,
    );
  

  List<Widget> _buildScreens()
    return [
      MyHomePage(),
    ];
  

  List<PersistentBottomNavBarItem> _navBarsItems()
    return [
      PersistentBottomNavBarItem(
        icon: Icon(Icons.home),
        title: ("Home"),
        activeColorPrimary: Color(0xFF254690),
        inactiveColorPrimary: Color(0xFFD3DAE9),
      ),
      PersistentBottomNavBarItem(
        icon: Icon(Icons.chat_rounded),
        title: ("Chat"),
        activeColorPrimary: Color(0xFF254690),
        inactiveColorPrimary: Color(0xFFD3DAE9),
      ),
    ];
  

▽ main.dart

import 'package:flutter/material.dart';

import 'package:project1_makers/bottomNav.dart';

void main() => runApp(MyApp());

class MyApp extends StatelessWidget 
  @override
  Widget build(BuildContext context) 
    return MaterialApp(
      title: 'Flutter Demo',
      theme: ThemeData(primaryColor: Colors.white),
      debugShowCheckedModeBanner: false,
      home: MyHomePage(),
    );
  


class MyHomePage extends StatefulWidget 
  @override
  State<StatefulWidget> createState() => _MyHomePageState();


class _MyHomePageState extends State<MyHomePage> 
  @override
  Widget build(BuildContext context) 
    return Scaffold(
      body: Stack(
        children: <Widget>[
          bottomNav(),
        ],
      )
    );
  

▽结果

══╡ EXCEPTION CAUGHT BY WIDGETS LIBRARY ╞═══════════════════════════════════════════════════════════
The following assertion was thrown building bottomNav(dirty):
NavBar styles 15-18 only accept 3 or 5 PersistentBottomNavBarItem items.
'package:persistent_bottom_nav_bar/persistent-tab-view.widget.dart':
Failed assertion: line 125 pos 12: 'assertMidButtonStyles(navBarStyle, items!.length)'

The relevant error-causing widget was:
  bottomNav [My_direction]

When the exception was thrown, this was the stack:
#2      new PersistentTabView (package:persistent_bottom_nav_bar/persistent-tab-view.widget.dart:125:12)
#3      bottomNav.build (package:project1_makers/bottomNav.dart:11:12)
#4      StatelessElement.build (package:flutter/src/widgets/framework.dart:4648:28)
#5      ComponentElement.performRebuild (package:flutter/src/widgets/framework.dart:4574:15)
#6      Element.rebuild (package:flutter/src/widgets/framework.dart:4267:5)
#7      ComponentElement._firstBuild (package:flutter/src/widgets/framework.dart:4553:5)
#8      ComponentElement.mount (package:flutter/src/widgets/framework.dart:4548:5)
#9      Element.inflateWidget (package:flutter/src/widgets/framework.dart:3611:14)
#10     MultiChildRenderObjectElement.inflateWidget (package:flutter/src/widgets/framework.dart:6221:36)
#11     MultiChildRenderObjectElement.mount (package:flutter/src/widgets/framework.dart:6232:32)
...     Normal element mounting (19 frames)
#30     Element.inflateWidget (package:flutter/src/widgets/framework.dart:3611:14)
#31     MultiChildRenderObjectElement.inflateWidget (package:flutter/src/widgets/framework.dart:6221:36)
#32     MultiChildRenderObjectElement.mount (package:flutter/src/widgets/framework.dart:6232:32)
...     Normal element mounting (238 frames)
#270    Element.inflateWidget (package:flutter/src/widgets/framework.dart:3611:14)
#271    MultiChildRenderObjectElement.inflateWidget (package:flutter/src/widgets/framework.dart:6221:36)
#272    MultiChildRenderObjectElement.mount (package:flutter/src/widgets/framework.dart:6232:32)
...     Normal element mounting (345 frames)
#617    Element.inflateWidget (package:flutter/src/widgets/framework.dart:3611:14)
#618    Element.updateChild (package:flutter/src/widgets/framework.dart:3363:18)
#619    RenderObjectToWidgetElement._rebuild (package:flutter/src/widgets/binding.dart:1189:16)
#620    RenderObjectToWidgetElement.mount (package:flutter/src/widgets/binding.dart:1159:5)
#621    RenderObjectToWidgetAdapter.attachToRenderTree.<anonymous closure> (package:flutter/src/widgets/binding.dart:1104:18)
#622    BuildOwner.buildScope (package:flutter/src/widgets/framework.dart:2535:19)
#623    RenderObjectToWidgetAdapter.attachToRenderTree (package:flutter/src/widgets/binding.dart:1103:13)
#624    WidgetsBinding.attachRootWidget (package:flutter/src/widgets/binding.dart:937:7)
#625    WidgetsBinding.scheduleAttachRootWidget.<anonymous closure> (package:flutter/src/widgets/binding.dart:917:7)
(elided 13 frames from class _AssertionError, class _RawReceivePortImpl, class _Timer, dart:async, and dart:async-patch)

我想用 NavBarStyle 15,但是很难申请。

你能帮我解决这个错误吗?

如果您指出我的描述中缺少某些内容,我们将不胜感激。

提前致谢:)

【问题讨论】:

【参考方案1】:

这很容易解决,仔细查看错误消息本身,它说如下:NavBar 样式 15-18 只接受 3 或 5 个 PersistentBottomNavBarItem 项。 但是您的_navBarsItems 列表中只有 2 个 PersistentBottomNavBarItems。

【讨论】:

以上是关于断言失败:第 125 行第 12 行:'assertMidButtonStyles(navBarStyle, items!.length)'的主要内容,如果未能解决你的问题,请参考以下文章

颤动:使用navigator.dart时失败断言

错误:未布置 RenderBox,断言失败:第 1940 行 pos 12:'hasSize'

断言失败:第 551 行 pos 12:'child.hasSize':不正确

断言失败:第 6075 行 pos 12:'child == _child':不正确

断言行 5120 pos 12 失败:'child = _child' 不正确

断言失败:第 22 行 pos 14: 'url != null': is not true