【问题标题】:Why does `const` modifier avoid StreamBuilder rebuild in Flutter?为什么 `const` 修饰符避免在 Flutter 中重建 StreamBuilder?
【发布时间】:2022-01-09 16:44:37
【问题描述】:

在我的应用主页中,我调用了一个 ScoreListPageBody,当我从另一个页面 (SettingsPage) 更改区域设置时,它会重新构建。

为了强制重建它,它包含一个 StreamBuilder,当区域设置更改并且用户返回主页时触发(使用 Navigator.push(....).then(() => triggerStream())) .

当我在主页中执行此操作时效果很好:

return Scaffold(
    appBar: MenuAppBar(),
    body: PageView(
        controller: this._pageController,
        children: [
            ScoreListPageBody(),
            FavoritesListPageBody(),
        ],
    )
);

但这会阻止 ScoreListPageBody 的 StreamBuilder 的重建:

return Scaffold(
    appBar: MenuAppBar(),
    body: PageView(
        controller: this._pageController,
        children: const [
            ScoreListPageBody(),
            FavoritesListPageBody(),
        ],
    )
);

我不明白为什么,因为 const 修饰符不应该影响 StreamBuilder 的行为,对吧?

所以,它可以在没有 const 的情况下工作,但是向 Flutter 团队报告是个问题吗?

【问题讨论】:

    标签: flutter stream-builder


    【解决方案1】:

    const 修饰符会影响 StreamBuilder,因为它位于 ScoreListPageBody() 内部,并且您将修饰符放在树上方一级的 PageView 中。

    - PageView
    -- ScoreList()
    -- ...
    

    【讨论】:

    • 我知道,但是为什么 const 修饰符会影响 StreamBuilder ? ScoreListPageBody 构造函数是常量,我不明白为什么声明 const ScoreListPageBody() 会改变这种行为...
    猜你喜欢
    • 1970-01-01
    • 2015-11-10
    • 2023-04-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-03-26
    • 1970-01-01
    • 2011-04-12
    相关资源
    最近更新 更多