Why does `const` modifier avoid StreamBuilder rebuild in Flutter?
Asked Answered
S

1

0

In my app's homepage I call a ScoreListPageBody that is rebuild when I change the Locale from another page (SettingsPage).

To force rebuild it, it contains a StreamBuilder that is triggered when the Locale changes and the users goes back to the HomePage (using Navigator.push(....).then(() => triggerStream())).

It works very well when I do this in my HomePage :

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

But this prevents the rebuild of ScoreListPageBody's StreamBuilder :

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

I do not understand why, as const modifier should not impact the StreamBuilder's behavior, right ?

So, it works without const, but is it an issue to report to Flutter team ?

Specialism answered 9/1, 2022 at 16:44 Comment(0)
I
0

The const modifier impact the StreamBuilder because this is inside ScoreListPageBody() and you put the modifier in the PageView which is one step above the tree.

- PageView
-- ScoreList()
-- ...
Immorality answered 9/1, 2022 at 18:33 Comment(1)
I know that but why does a const modifier influence a StreamBuilder ? The ScoreListPageBody constructor is constant and I don't understand why declaring const ScoreListPageBody() changes this behavior...Specialism

© 2022 - 2024 — McMap. All rights reserved.