How does Flutter Widget rerender works when it's properties change?

1,949

So in code above, the NewLocationState comes every 15 sec with new values.

Does Flutter renders a completely new Map Widget every 15sec?

Yes. Widgets are aren't reused in any way.

On the other hand, while the widget instance is recreated, its associated State & Element & RenderObjects are preserved (as they are mutable instead).

Since it's these three that does all the work in a Flutter app, it is actually pretty efficient.

Share:
1,949
Author by

OMGImOut

Updated on December 16, 2022

Comments

  • OMGImOut 1 minute

    I'm just wondering how Flutter Widget works when Parent widget's properties change.

    Let's say we have a Parent Widget which renders Widget base on some mutable value or some listenable value (like in Bloc Pattern).

    return Scaffold(
      body: BlocBuilder<LocationBloc, LocationState>(builder: (context, state) {
        if (state is NewLocationState) {
          return Map(
            location: LatLng(state.latitude, state.longitude),
          );
        }
        return Container();
      }),
    );
    

    So in code above, the NewLocationState comes every 15 sec with new values.

    My question is: What is happening with this Map Widget?

    Does Flutter renders a completely new Map Widget every 15sec with new NewLocationState or similar to React, Flutter can compare the changes in some kind of virtual tree and only change the properties in an existing Map which is efficient and should cause less memory consumption.

    Bloc example is just for reference/context of my question. but I want to know overall how Flutter Widget behaves when properties change.

    Cheers

  • OMGImOut almost 3 years
    Thanks, so better practice should be instanciate Map Widget once without state assertion and move BlocProvider inside Map widget or it doesn't matter as this way I'll call setState on NewLocationState inside Widget and Widget will rerender anyway?
  • Rémi Rousselet
    Rémi Rousselet almost 3 years
    It doesn't matter.
  • Chinky Sight almost 2 years
    @RémiRousselet well I have a doubt. As you said, Widgets aren't reused, so when the update occurs what happens to the old widget. Does Flutter dispose it or it will be still on the memory ?