0

This issue occurred the first time I ran my app today. After some testing I found that the methods I call in the Widget build section only run once on the intital build or hot reload. Previously, these methods would constantly be running. For example, if I printed an integer with the value of 5 in one of the methods, the console would display:

I/flutter (  510): 5
I/flutter (  510): 5
I/flutter (  510): 5
I/flutter (  510): 5
I/flutter (  510): 5

etc...

Now the console only prints the integer once (Until I hot reload):

I/flutter (  510): 5
  • Even with an async method it will only print once to the console.
  • The same situation applies to error messages.
  • It only appears to be broken on the main page.
  • This issue occurs on my other apps as well.
  • I have ran flutter clean in the console.
Doctor summary (to see all details, run flutter doctor -v):
[√] Flutter (Channel unknown, v1.12.13+hotfix.4, on Microsoft Windows [Version 10.0.18363.778], locale en-US)

[√] Android toolchain - develop for Android devices (Android SDK version 29.0.1)
[√] Android Studio (version 3.4)
[√] VS Code (version 1.44.2)
[√] Connected device (1 available)

• No issues found!

My question would be if I was implementing code like this correctly in the first place? Is there another way I should be doing this, or is flutter broken?

Thanks, I appreciate the help!

@override
  Widget build(BuildContext context) {

    // This code only runs once on the inital build
    createDay();
    getSaveContent();
    _updatePointsProgress();
    printStuff();

    return MaterialApp(
        home: Builder(
Sam Scolari
  • 113
  • 1
  • 10

1 Answers1

0

I am not sure if I am misunderstanding what you are thinking or not but if what you are implying is that your functions are being skipped over after the first build method that simple is not possible. Like this.

@override
  Widget build(BuildContext context) {
   //Some code here runs once 

  //Some code here is running more often than above code 
    return MaterialApp(
        home: Builder(

This is not whats happening. Before you probably had something going on that was causing rebuilds and now your widget isn't rebuilding. Your build function is like any other function it executes every line of code top to bottom. To answer your question you should be implementing code in your build method that can run at anytime with out any negative side effects. There is a good Stack thread already about keeping build methods clean I will link it. Initialization code should go at the very least outside build method but more ideally in initState(). Let me know if this didn't answer your question.

Build Method Should be Pure

wcyankees424
  • 2,554
  • 2
  • 12
  • 24