10

I have this very basic test app:

import 'package:flutter/material.dart';

void main() => runApp(MyApp());

class MyApp extends StatelessWidget {

  @override
  Widget build(BuildContext context) {
    print("ROOT WIDGET ");
    return MaterialApp(
      title: 'Flutter Demo',
      theme: ThemeData(
        primarySwatch: Colors.blue,
      ),
      home: Container(decoration: BoxDecoration(color: Colors.blue),),
    );
  }
}

Log output debug:

Launching lib/main.dart on iPhone X in debug mode...

Found saved certificate choice "XXX". To clear, use "flutter config". Signing iOS app for device deployment using developer identity: "XXX"

Running Xcode build...

Xcode build done. 39,4s

Installing and launching...

flutter: ROOT WIDGET

Syncing files to device iPhone X...

flutter: ROOT WIDGET

Log output with flutter run --release

Launching lib/main.dart on iPhone X in release mode...

Found saved certificate choice "XXX". To clear, use "flutter config".

Signing iOS app for device deployment using developer identity: "XXX"

Running pod install...
1,2s

Running Xcode build...
├─Building Dart code... 15,8s

├─Generating dSYM file... 0,1s

├─Stripping debug symbols... 0,0s

├─Assembling Flutter resources... 0,7s

└─Compiling, linking and signing... 48,3s

Xcode build done. 67,4s

Installing and launching...
6,8s

To quit, press "q".

flutter: ROOT WIDGET

As you can see it only happens in debug mode.

The reason why I ask is, in my real app, I use a WebView. The function onWebViewCreated(WebViewController controller) of the WebView is only called the first time, so that the WebViewController is always null in debug mode.

最白目
  • 3,505
  • 6
  • 59
  • 114
  • 1
    It doesn't matter and you shouldn't bother about this. If it cause issues for you, then you're likely doing something you shouldn't inside your _build_ method. See https://stackoverflow.com/questions/52249578/how-to-deal-with-unwanted-widget-build – Rémi Rousselet Apr 04 '19 at 11:58
  • Thanks for the quick answer, but as you can see I´m doing bascially nothing in my build method. – 最白目 Apr 04 '19 at 12:02
  • Oh I think I see what you mean, in the other app I initialize a webView in the build method, that´s probably something I shouldn´t do. But then, I have to initialize my webView somewhere, where if not in the build method. – 最白目 Apr 04 '19 at 12:04
  • That's likely it :) You'll want to do that inside an _initState_ or similar – Rémi Rousselet Apr 04 '19 at 12:05
  • It works now. You saved me from going crazy! In fact, your linked answer is so good that I just sent it as a note to my entire team. Thanks! – 最白目 Apr 04 '19 at 12:39
  • Could you clarify the solution - to turn MyApp into a stateful widget and call init methods from initState? I have the same issue with redux and the solution is a bit unclear from this conversation. – Alexander Semenov Jun 03 '19 at 19:05
  • 1
    @Alexander Semenov Yes in this particilular case with the webview we only initialized it in the initState method and the problem didnt occur anymore. – 最白目 Jun 04 '19 at 05:30
  • Nice catch, might be worth to post this as answer – Alexander Semenov Jun 05 '19 at 13:37

3 Answers3

7

I have been getting this issue on

Flutter 1.22.0 • channel stable

Deleting the app from my emulator/physical phone and reinstalling fixes the issue. Delete the app from the emulator/physical from System Settings/Apps and reinstall in debug mode.

I have no idea why but time to time my main widget keeps on getting rebuild exactly twice and I dont do anything in my main widget no SetStates or no futurebuilder. I think this is a flutter internal bug.

cs guy
  • 926
  • 2
  • 13
  • 33
1

The clue might be here

Installing and launching... //We are launching the app , the code bundled with it it's executed briefly before it "sync" to allow hot reload

flutter: ROOT WIDGET //The bundled code executes

Syncing files to device iPhone X... //We start the sync, hot reload triggers an app rebuild 

flutter: ROOT WIDGET //The live code executes

If you comment the print, then hot-reload, then uncomment the print, you should only see one "ROOT WIDGET".

RegularGuy
  • 3,516
  • 1
  • 12
  • 29
-3

I have same issue, and I found that it's not flutter's problem, it's vscode. try running in android studio.

Ershat
  • 29
  • 4
  • How can it help fix the issue with the environment in the question? – s0xzwasd Jul 09 '21 at 16:38
  • I don't know exactly, maybe my answer is not related this issue. but when I was using vscode, app builded twice every time. it does not happen on android studio – Ershat Jul 30 '21 at 06:55