Make itemBuilder run every time without calling setState()









up vote
0
down vote

favorite












I am having a code like this for PageView.builder



@override
Widget build(BuildContext context)
return PageView.builder(
itemCount: myList.length,
onPageChanged: (int pageIndex) => setState(() => index = pageIndex), // this make it work fine
itemBuilder: (context, index) => MyPage(myList[index])
);



The problem with this itemBuilder (of PageView.builder) and normal itemBuilder (of ListView.builder) is that they stop getting called once they have drawn the widget on the screen for previous index value.



Now in my use case, I want this itemBuilder to fire every time the Page has changed in the PageView because I am updating list and now I want to show new data in MyPage for new list. How to do that?




PS: There is onPageChanged() callback and I can use setState() in that, it works fine but it simply ruins the purpose of PageView OR is it okay to use setState() in onPageChanged(), wont' that be redundant?










share|improve this question























  • so you have to rebuild your PageView as there is no "animated" alternative like AnimatedList for static ListView
    – pskink
    Nov 17 at 9:31










  • So, I ended up calling setState() to solve the issue. Not a nice solution but worked for me.
    – Volley Ball
    Nov 17 at 9:33










  • you dont have to use any onPageChanged and call setState - just make sure PageView is rebuilt - like here
    – pskink
    Nov 17 at 9:38










  • @pskink I am sorry, I didn't quite try it yet, I am still using my solution of calling setState() from onPageChanged().
    – Volley Ball
    Nov 17 at 11:47














up vote
0
down vote

favorite












I am having a code like this for PageView.builder



@override
Widget build(BuildContext context)
return PageView.builder(
itemCount: myList.length,
onPageChanged: (int pageIndex) => setState(() => index = pageIndex), // this make it work fine
itemBuilder: (context, index) => MyPage(myList[index])
);



The problem with this itemBuilder (of PageView.builder) and normal itemBuilder (of ListView.builder) is that they stop getting called once they have drawn the widget on the screen for previous index value.



Now in my use case, I want this itemBuilder to fire every time the Page has changed in the PageView because I am updating list and now I want to show new data in MyPage for new list. How to do that?




PS: There is onPageChanged() callback and I can use setState() in that, it works fine but it simply ruins the purpose of PageView OR is it okay to use setState() in onPageChanged(), wont' that be redundant?










share|improve this question























  • so you have to rebuild your PageView as there is no "animated" alternative like AnimatedList for static ListView
    – pskink
    Nov 17 at 9:31










  • So, I ended up calling setState() to solve the issue. Not a nice solution but worked for me.
    – Volley Ball
    Nov 17 at 9:33










  • you dont have to use any onPageChanged and call setState - just make sure PageView is rebuilt - like here
    – pskink
    Nov 17 at 9:38










  • @pskink I am sorry, I didn't quite try it yet, I am still using my solution of calling setState() from onPageChanged().
    – Volley Ball
    Nov 17 at 11:47












up vote
0
down vote

favorite









up vote
0
down vote

favorite











I am having a code like this for PageView.builder



@override
Widget build(BuildContext context)
return PageView.builder(
itemCount: myList.length,
onPageChanged: (int pageIndex) => setState(() => index = pageIndex), // this make it work fine
itemBuilder: (context, index) => MyPage(myList[index])
);



The problem with this itemBuilder (of PageView.builder) and normal itemBuilder (of ListView.builder) is that they stop getting called once they have drawn the widget on the screen for previous index value.



Now in my use case, I want this itemBuilder to fire every time the Page has changed in the PageView because I am updating list and now I want to show new data in MyPage for new list. How to do that?




PS: There is onPageChanged() callback and I can use setState() in that, it works fine but it simply ruins the purpose of PageView OR is it okay to use setState() in onPageChanged(), wont' that be redundant?










share|improve this question















I am having a code like this for PageView.builder



@override
Widget build(BuildContext context)
return PageView.builder(
itemCount: myList.length,
onPageChanged: (int pageIndex) => setState(() => index = pageIndex), // this make it work fine
itemBuilder: (context, index) => MyPage(myList[index])
);



The problem with this itemBuilder (of PageView.builder) and normal itemBuilder (of ListView.builder) is that they stop getting called once they have drawn the widget on the screen for previous index value.



Now in my use case, I want this itemBuilder to fire every time the Page has changed in the PageView because I am updating list and now I want to show new data in MyPage for new list. How to do that?




PS: There is onPageChanged() callback and I can use setState() in that, it works fine but it simply ruins the purpose of PageView OR is it okay to use setState() in onPageChanged(), wont' that be redundant?







dart flutter






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 11 at 16:47

























asked Nov 11 at 16:16









Volley Ball

426214




426214











  • so you have to rebuild your PageView as there is no "animated" alternative like AnimatedList for static ListView
    – pskink
    Nov 17 at 9:31










  • So, I ended up calling setState() to solve the issue. Not a nice solution but worked for me.
    – Volley Ball
    Nov 17 at 9:33










  • you dont have to use any onPageChanged and call setState - just make sure PageView is rebuilt - like here
    – pskink
    Nov 17 at 9:38










  • @pskink I am sorry, I didn't quite try it yet, I am still using my solution of calling setState() from onPageChanged().
    – Volley Ball
    Nov 17 at 11:47
















  • so you have to rebuild your PageView as there is no "animated" alternative like AnimatedList for static ListView
    – pskink
    Nov 17 at 9:31










  • So, I ended up calling setState() to solve the issue. Not a nice solution but worked for me.
    – Volley Ball
    Nov 17 at 9:33










  • you dont have to use any onPageChanged and call setState - just make sure PageView is rebuilt - like here
    – pskink
    Nov 17 at 9:38










  • @pskink I am sorry, I didn't quite try it yet, I am still using my solution of calling setState() from onPageChanged().
    – Volley Ball
    Nov 17 at 11:47















so you have to rebuild your PageView as there is no "animated" alternative like AnimatedList for static ListView
– pskink
Nov 17 at 9:31




so you have to rebuild your PageView as there is no "animated" alternative like AnimatedList for static ListView
– pskink
Nov 17 at 9:31












So, I ended up calling setState() to solve the issue. Not a nice solution but worked for me.
– Volley Ball
Nov 17 at 9:33




So, I ended up calling setState() to solve the issue. Not a nice solution but worked for me.
– Volley Ball
Nov 17 at 9:33












you dont have to use any onPageChanged and call setState - just make sure PageView is rebuilt - like here
– pskink
Nov 17 at 9:38




you dont have to use any onPageChanged and call setState - just make sure PageView is rebuilt - like here
– pskink
Nov 17 at 9:38












@pskink I am sorry, I didn't quite try it yet, I am still using my solution of calling setState() from onPageChanged().
– Volley Ball
Nov 17 at 11:47




@pskink I am sorry, I didn't quite try it yet, I am still using my solution of calling setState() from onPageChanged().
– Volley Ball
Nov 17 at 11:47

















active

oldest

votes











Your Answer






StackExchange.ifUsing("editor", function ()
StackExchange.using("externalEditor", function ()
StackExchange.using("snippets", function ()
StackExchange.snippets.init();
);
);
, "code-snippets");

StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "1"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);

else
createEditor();

);

function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader:
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
,
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);



);













draft saved

draft discarded


















StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53250653%2fmake-itembuilder-run-every-time-without-calling-setstate%23new-answer', 'question_page');

);

Post as a guest















Required, but never shown






























active

oldest

votes













active

oldest

votes









active

oldest

votes






active

oldest

votes















draft saved

draft discarded
















































Thanks for contributing an answer to Stack Overflow!


  • Please be sure to answer the question. Provide details and share your research!

But avoid


  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.

To learn more, see our tips on writing great answers.





Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


Please pay close attention to the following guidance:


  • Please be sure to answer the question. Provide details and share your research!

But avoid


  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.

To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53250653%2fmake-itembuilder-run-every-time-without-calling-setstate%23new-answer', 'question_page');

);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







這個網誌中的熱門文章

Barbados

How to read a connectionString WITH PROVIDER in .NET Core?

Node.js Script on GitHub Pages or Amazon S3