Summary of some knowledge concepts in Flutter

After studying Flutter for a while, there are some conceptual things that I still plan to write down and record.

Widget、Element、RenderObject

Everyone knows that Widget, Element, RenderObject, each part is responsible for the corresponding function, we only need to write Widget, why do you need Element and RenderObject, this is because Flutter can not always delete the creation in order to ensure good performance when a large number of Widget The new object can reuse the original object, such as reuse Element, and then only need to modify the content of RenderObject, which can reduce performance loss and improve fluency.
Insert picture description here

Flutter Engine

In the new version of the Flutter API, we can use source code-dependent methods and native mixed development. It mentions that you can preheat a Flutter Engine, and then save it in the cache. You will use this Flutter Engine to process the Flutter page later, so you can avoid creating Multiple Flutter Engines reduce memory consumption.

So what is Flutter Engine?

Flutter Engine is implemented by C ++ and is responsible for the following functions:

  • Thread management
  • Dart VM state management
  • Dart code loading, the loaded code runs in separate Isolate

Dart VM

Where to run Dart code

Isolate

Similar to threads in Java, but Isolate does not share memory, and sends and receives data through Port, so there is no problem with locks.

Widget update mechanism

When the configuration data of a parent Widget changes, and the Widget structure returned by its State.build is different from the previous one, it is necessary to rebuild the corresponding Element tree. In order to reuse Element, it will first try to reuse the element at the same position on the old tree before the Element is rebuilt. The element node will call the corresponding Widget's canUpdate method before updating. If it returns true, then reuse the old Element, The old Element will be updated with the new Widget configuration data, otherwise it will create a new Element. Widget.canUpdate is mainly to determine whether the runtimeType and key of newWidget and oldWidget are equal at the same time. If they are equal at the same time, it returns true, otherwise it returns false. According to this principle, when we need to force update a Widget, we can avoid multiplexing by specifying a different Key.

Published 82 original articles · Like 86 · Visit 110,000+

Guess you like

Origin blog.csdn.net/unicorn97/article/details/105254356