【发布时间】:2019-07-28 14:17:31
【问题描述】:
当使用BottomAppBar 时,我想通过不同的页面传递动态数据。我目前在页面/小部件之间切换,我的存储方式如下:
@override
void initState() {
super.initState();
final _pageOptions = [
SwipeScreen(currentUserId: currentUserId),
Requests(currentUserId: currentUserId),
Messages(currentUserId: currentUserId),
Settings(),
];}
然后我在我的Scaffold 中使用_pageOptions:
body: _pageOptions[_selectedPage],
bottomNavigationBar: BottomNavigationBar(
currentIndex: _selectedPage,
onTap: (int index) {
setState(() {
_selectedPage = index;
});
},
正如您可能已经猜到的那样,我不能在Scaffold 中使用_pageOptions 作为我的主体,因为它在包裹在initState() 中时是一个局部变量。我必须使用这个 initState,因为没有它,我只能在我的初始化程序中传递静态成员。
我不知道如何解决这个问题,因为删除一个只会给我一个不同的错误。我一直在寻找使变量全局化的方法,例如在不同的文件中使用 _pageOptions,但它仍然是本地的,因此在我的 Scaffold 中使用时没有定义。
我希望我的问题很清楚。
提前致谢。
【问题讨论】: