FlatBuffers
An open source project by FPL.
|
Before diving into the FlatBuffers usage in Dart, it should be noted that the Tutorial page has a complete guide to general FlatBuffers usage in all of the supported languages (including Dart). This page is designed to cover the nuances of FlatBuffers usage, specific to Dart.
You should also have read the Building documentation to build flatc
and should be familiar with Using the schema compiler and Writing a schema.
The code for the FlatBuffers Dart library can be found at flatbuffers/dart
. You can browse the library code on the FlatBuffers GitHub page.
The code to test the Dart library can be found at flatbuffers/tests
. The test code itself is located in dart_test.dart.
To run the tests, use the DartTest.sh shell script.
Note: The shell script requires the Dart SDK to be installed.
Note: See Tutorial for a more in-depth example of how to use FlatBuffers in Dart.
FlatBuffers supports reading and writing binary FlatBuffers in Dart.
To use FlatBuffers in your own code, first generate Dart classes from your schema with the --dart
option to flatc
. Then you can include both FlatBuffers and the generated code to read or write a FlatBuffer.
For example, here is how you would read a FlatBuffer binary file in Dart: First, include the library and generated code. Then read a FlatBuffer binary file into a List<int>
, which you pass to the factory constructor for Monster
:
Now you can access values like this:
The work in this repository is signfiicantly based on the implementation used internally by the Dart SDK in the front end/analyzer package. Several significant changes have been made.
There currently is no support for parsing text (Schema's and JSON) directly from Dart, though you could use the C++ parser through Dart Native Extensions. Please see the C++ documentation for more on text parsing (note that this is not currently an option in Flutter - follow this issue for the latest).
FlatBuffers is all about memory efficiency, which is why its base API is written around using as little as possible of it. This does make the API clumsier (requiring pre-order construction of all data, and making mutation harder).
For times when efficiency is less important a more convenient object based API can be used (through --gen-object-api
) that is able to unpack & pack a FlatBuffer into objects and lists, allowing for convenient construction, access and mutation.
To use: