gpt4 book ai didi

flutter - 尝试向底部导航栏添加索引时,“The method '> ' was called on null.”

转载 作者:行者123 更新时间:2023-12-03 04:00:31 24 4
gpt4 key购买 nike

尝试添加currentIndex-Property时出现此错误。

我很确定我在某些较旧的项目中确实做到了这一点,并且不知道为什么它不起作用。

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)

最佳答案

我今天也遇到了这个确切的问题。我没有答案的原因,但我可以描述导致其消失的奇异事件链。

我的代码与您的代码几乎相同。我使用_selectedIndex作为变量名,而不是使用_currentIndex。如果我切换:

currentIndex: _selectedIndex,


currentIndex: 0,

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

最终,我最终尝试了“随机”操作来纠正此问题。最终对我有用的是将变量名从_selectedIndex更改为selectedIndex。是的,就是这样。然后,更奇怪的是,当我之后立即将其切换回_selectedIndex时,错误不再出现。

我对flutter SDK的内部了解不足,无法假设正在发生的事情,所以我不会。这对我有用,并希望将来对其他人有用。

关于flutter - 尝试向底部导航栏添加索引时,“The method '> ' was called on null.”,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57485666/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com