How does Flutter Widget rerender works when it's properties change?
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.
OMGImOut
Updated on December 16, 2022Comments
-
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 newNewLocationState
or similar to React, Flutter can compare the changes in some kind of virtual tree and only change the properties in an existingMap
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
-
Harshvardhan Joshi almost 3 yearsFor a complete reference on how the flutter renders the widgets refer to this video: youtube.com/watch?v=996ZgFRENMs
-
-
OMGImOut almost 3 yearsThanks, so better practice should be instanciate
Map
Widget once without state assertion and move BlocProvider insideMap
widget or it doesn't matter as this way I'll call setState onNewLocationState
inside Widget and Widget will rerender anyway? -
Rémi Rousselet almost 3 yearsIt 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 ?