尝试将索引添加到底部导航栏时"The method '>' was called on null."



当我尝试添加当前索引属性时,我收到此错误。

我很确定我在某些较旧的项目中完全是这样做的,并且不明白为什么它不起作用。

int _currentIndex = 0;
bottomNavigationBar: BottomNavigationBar(
currentIndex: _currentIndex,
onTap: (int index) {
setState(() {
_currentIndex = index; 
});
},
items: [
BottomNavigationBarItem(
icon: Icon(Icons.bubble_chart),
title: Text("Home")
),
BottomNavigationBarItem(
icon: Icon(Icons.timelapse),
title: Text("Timeline")
),
BottomNavigationBarItem(
icon: Icon(Icons.person_outline),
title: Text("Home")
)
],
),

错误输出为

I/flutter (11694): The method '>' was called on null.
I/flutter (11694): Receiver: null
I/flutter (11694): Tried calling: >(0)

我今天也遇到了这个确切的问题。对于是什么原因造成的,我没有答案,但我可以描述导致它消失的离奇事件链。

我的代码几乎和你的一样。我没有使用 _currentIndex,而是使用 _selectedIndex 作为我的变量名。如果我切换:

currentIndex: _selectedIndex,

currentIndex: 0,

错误消失了。(是的,_selectedIndex正在初始化为 0)

最终,我最终尝试了"随机"的东西来纠正这个问题。最终对我有用的是将变量名称从 _selectedIndex 更改为 selectedIndex。是的,就是这样。然后,更奇怪的是,当我立即将其切换回_selectedIndex时,错误没有回来。

我对颤振 SDK 的内部结构没有足够的了解来假设发生了什么,所以我不会。这对我有用,希望将来对其他人有用。

不幸的是,我也遇到了这个问题!

我发现我的_currentIndexnull,你自己检查一下就行了!

就我而言,_currentIndex由其构造函数分配以指定初始索引,我在调用它时忘记输入该索引。

相关内容

最新更新