如何解决Flutter重建消费者的父窗口小部件
Widget build(BuildContext context) {
print("homepage");
final navigation = Provider.of<NavigationProvider>(context,listen: false);
return Consumer<ThemeProvider>(builder: (context,themeData,_) {
return Scaffold(
backgroundColor: themeData.getTheme['mainBackground'],appBar: AppBar(
backgroundColor: themeData.getTheme['appBar'],elevation: 0.0,title: Text(
"INTRADAE",style: GoogleFonts.montserrat(
color: themeData.getTheme['textColor'],),actions: <Widget>[
Container(
margin: EdgeInsets.only(right: 5),child: IconButton(
icon: Icon(
Icons.account_balance_wallet,size: 30,color: themeData.getTheme['textColor'],onPressed: null,)
],body: Consumer<NavigationProvider>(
builder: (context,navigationProvider,_) =>
navigationProvider.getNavigation),bottomNavigationBar: CurvedNavigationBar(
index: 0,height: 50,color: themeData.getTheme['secondaryBackground'],backgroundColor: themeData.getTheme['mainBackground'],buttonBackgroundColor: themeData.getTheme['mainBackground'],items: <Widget>[
Icon(Icons.home,color: Colors.white),Icon(Icons.shopping_cart,Icon(Icons.person,color: Colors.white)
],onTap: (index) {
navigation.updateNavigation(_pages[index]);
},);
});
updateNavigation
发生时,我正在尝试更新脚手架内部的内容。一切似乎都很好,直到我在主窗口小部件的build方法中放入print
为止,并注意到在每次NavigationProvider
的更新中都调用了它。
从那以后我挠头。我不明白为什么只有当消费者需要重建时才必须重建此小部件,在这种情况下,该部件位于脚手架的主体内。
从React的背景来看,这对我来说似乎很奇怪。
谁能告诉我为什么会这样吗?
解决方法
执行热重启后,问题已解决。然后,我在Flutter文档中阅读了有关“热重装”的信息,并意识到除非执行热重启动,否则不会看到对代码所做的更改。
尽管此处发布的代码具有
听:假
在
Provider.of
(上下文,监听:false)
但是,我本来在代码中没有。当添加该论点似乎也无济于事时,我决定在此处发布一个问题。 问题一直是热装。在发布之前应该已经确定。
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。