Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Flutter - How does it work behind the scenes?

Tags:

flutter

dart

Obviously, flutter is a framework for running apps on iOS and Android using one codebase. But how does it manage to do so? Will it compile to native code, or is there a mid level language or a VM involved in the process? I tried reading some of the source code on Github, but I couldn't find anything.

like image 811
OhMad Avatar asked May 08 '17 18:05

OhMad


People also ask

How does Flutter work under the hood?

The tree detects our objects and automatically updates the layout for its changes. Flutter widgets are reactive. They respond to any new information from an outside source. Consider a Stateful component as a parent to another Stateless component.

Does Flutter run natively?

Flutter is Google's portable UI toolkit for crafting beautiful, natively compiled applications for mobile, web, and desktop from a single codebase.

Why you should not use Flutter?

One of the biggest drawbacks to Flutter is Dart, its implementation language. Dart is one of the languages you can use if you're running Google's web or back-end hosting environments. And that's pretty much it.

How does Flutter run my code on Android?

Using the Dart language allows Flutter to compile the source code ahead-of-time to native code. The engine's C/C++code is compiled with Android's NDK or iOS' LLVM. Both pieces are wrapped in a “runner” Android and iOS project, resulting in an apk or ipa file respectively.


2 Answers

The Dart source code is compiled to native code using Dart's AoT compilation feature. It still needs parts of the the Dart VM (some runtime components like garbage collection) to run though, but the code is compiled to native code ahead of time, because iOS doesn't allow dynamic compilation.

Flutter can also call out to Android and use Android features only available in Java (same with iOS). Flutter supports to build custom plugins (in addition to a lot of provided ones) to call out to native platform code.

like image 75
Günter Zöchbauer Avatar answered Oct 02 '22 03:10

Günter Zöchbauer


The term Flutter refers to two major things/concepts

  1. Flutter SDK
  2. Flutter Framework

1.Flutter SDK: is a collection of tools that allows you to build any kind of app for both Android & iOS platform in one codebase.

2.Flutter Framework: Basically it provides all the predefined widgets/widget library, utility functions & packages.

We know flutter uses Dart as a programming language. And apps build/developed by flutter can be run on Android & iOS. And so that, we need to compile dart code to native machine android/ios code.

And the compilation task is done by Flutter SDK.

Blockquote

While talking about compilation, there are two kinds of operation/compilation

  • Static Compilation
  • Dynamic Interpretation

Static Compilation: Statically compiled programs are all translated into machine code before execution. Ex: AOT(Ahead of Time) - C/C++.

Dynamic Interpretation: is executed by one-to-one translation. Ex: JIT(Just in Time) - Javascript/Python.


Now we know how dart code convert into machine code, and two kinds of compilation.

But how these two kinds of compilation are related to Flutter ?

To know that, we need to know couple of things. Flutter doesn't use any kind of web view or native controls of the operating system. Instead flutter uses it's own high performance rendering engine(Skia), to draw widgets.

And the high performance is mainly guaranteed by two points.

  • Dart Language
  • Own engine to render/draw widgets

Now we are on the point finally. Before talking about dart language compilation process, I need to mention that JIT & AOT refer to the way the program runs, and the programming language isn't strongly related. And some languages support JIT and AOT together, Ex: Java, Python.

first time - Compiled -> intermediate byte code later -> byte code will be directly executed 

The DART runtime and compiler also support a combination of two key features - JIT & AOT.


??? IF YOU ARE INTERESTED TO KNOW ABOUT FLUTTER FRAME STRUCTURE ??? Pic Credit: book.flutterchina.club The bottom two layers (Foundation and Animation, Painting, Gestures) are merged into a dart UI layer, dart:ui.

Rendering layer is responsible to build UI tree and if any changes happen, compare and update the widget tree. And finally draw on the device screen(Something similar to React Virtual DOM).

Widgets layer is the basic component libraries provided by flutter.

And the top(Material, Cupertino) is a component library provided by flutter. And this is where/what we developers are dealing with most the time.

I think, now this is clear to all. Happy Coding !

Source: book.flutterchina.club, flutter.dev, maximilian schwarzmüller's flutter course, Flutter official YouTube channel.

like image 26
Robin Avatar answered Oct 02 '22 01:10

Robin