Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why Root Widget gets build twice?

Tags:

flutter

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.

like image 538
最白目 Avatar asked Apr 04 '19 11:04

最白目


People also ask

How do I stop widgets from rebuilding my flutter?

One of the easiest ways to avoid unwanted reBuilds that are caused usually by calling setState() in order to update only a specific Widget and not refreshing the whole page, is to cut that part of your code and wrap it as an independent Widget in another Stateful class.

What is root widget in flutter?

The MyApp is the root widget, and it is the parent of all the widgets in the tree. The MyApp widget renders the Container widget, and the Container widget renders the Row widget. The Row widget renders both Text and FlatButton widgets.

How does flutter use composition in building widgets?

Flutter uses composition in building widgets, i.e., widgets are composed of other widgets. These widgets make the app UI. The following is an example using composition to compose BusinessCardWidget widget. BusinessCardWidget is composed of CircleAvatar , Column , Row , and Text widgets.

How do you call a build method in flutter?

How build () method works how it rebuild itself flutter? Calling the setState() method in a stateful widget in Flutter calls the build() method first. Not only that, the build() method rebuilds all the descendant widgets. To enhance your fluttering performance, you need to understand the inner mechanism of Flutter.


2 Answers

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.

like image 63
cs guy Avatar answered Sep 22 '22 03:09

cs guy


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".

like image 32
ValdaXD Avatar answered Sep 22 '22 03:09

ValdaXD