Bottombarnavbar更改屏幕,更改包含我不同屏幕的数组的索引。当我尝试从导航抽屉中为此索引设置状态时出现错误:
我到了需要为索引设置状态的地步。我只复制了重要部分。
Drawer.dart
MyHomePage().setCurrentIndex(4);
main.dart
class MyHomePage extends StatefulWidget {
void setCurrentIndex(index){
_MyHomePageState().setCurrentIndex = index;
}
}
class _MyHomePageState extends State<MyHomePage>{
int currentIndex = 0;
void set setCurrentIndex(index){
currentIndex = index;
setState(() {
currentIndex = index;
print(index);
});
}
}
如果在没有设置状态的情况下打印出setCurrentIndex
中的索引,则索引将被正确打印,但是当我使用setState时出现此错误:
setState() called in constructor: _MyHomePageState#61a4e(lifecycle state: created, no widget, not mounted)