Compiled Product — bin
This article introduces you to the bin file of one of the compiled products. Where does # 1.bin come from?
You should know that Fair's design idea is to convert the layout-related code in the dart file into a DSL, and finally output a JSON file. JSON files can help us achieve dynamization.
However, we have found in practice that if you use JSON files for dynamic, you need to perform deserialization operations: initialize the parser and parse the data.
The deserialization operation not only prolongs the page load time, but also requires additional memory allocation. In terms of user experience and performance, there is a lot of room for optimization.
Therefore, we consider removing the deserialization operation.
The implementation method is to use FlatBuffers as a serialization tool. If you don’t know about FlatBuffers, you can read the official documentation first: Portal
In short, using FlatBuffers can avoid deserialization operations, which greatly improves the loading speed of dynamic pages.
The product of serialization using FlatBuffers is the bin file.
We compared the loading speed of the JSON file and the bin file (the left is the JSON file, the right is the bin file):
It can be seen that the loading speed of the bin file is much faster than that of the JSON file.
Since bin files are better than JSON in performance, why do compiled products still keep JSON files?
In fact, the reason is very simple, because JSON is very readable and can be used for debugging during debug. The bin file is a binary file, which is not readable, but has better performance and can be used in the release environment.
Therefore, we keep the output of the JSON file.
2. How to use the bin file?
The bin file is used in the same way as the JSON file, and it is also loaded using FairWidget:
FairWidget(
path: 'assets/fair/lib_main.fair.bin',
data: {
'fairProps': jsonEncode({'title': 'Hello'})
},
)
QA: Why is there no bin file in my build?
Generally speaking, if the bin file is not generated, it is due to the lack of the FlatBuffers environment. To install the FlatBuffers environment, refer to the following steps:
1. Install CMake
Download address: https://cmake.org/download/
Download and complete the default installation.

2. Download the source code of flatbuffers
Download address: https://github.com/google/flatbuffers.git
3. Open CMake software

After successful execution, the following files will be generated:

We cd into the flatcBuild
directory.
Then execute the make command
make //generate flatc

In this way, flatc is successfully generated, and then you need to install flatc
make install //install flatc
If you run into the following error:

Then change make install
to sudo make install
to solve it.
In this way, our FlatBuffers environment is installed.