Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ch06: The showData() method in build() causes repeated calls #19

Open
jialechan opened this issue Aug 1, 2023 · 0 comments
Open

ch06: The showData() method in build() causes repeated calls #19

jialechan opened this issue Aug 1, 2023 · 0 comments

Comments

@jialechan
Copy link

jialechan commented Aug 1, 2023

When I added print(shoppingList.length); this sentence to showData(), such as the following code:

The showData() method in build():

Widget build(BuildContext context) {
showData();
return Scaffold(

showData() source code:

Future showData() async {
await helper.openDb();
shoppingList = await helper.getLists();
setState(() {
shoppingList = shoppingList;
});
}

Add print(shoppingList.length); to showData()

Future showData() async { 
   await helper.openDb(); 
   shoppingList = await helper.getLists(); 
   // print here
   print(shoppingList.length);
   setState(() { 
     shoppingList = shoppingList; 
   }); 
 }

I found that showData() is actually being called repeatedly all the time.

It seems that showData is called in build, and setState in showData will cause build to be called again, which leads to a circular call.

This should be incorrect, right? Wouldn't this lead to a waste of resources due to non-stop rebuilding?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Development

No branches or pull requests

1 participant