Before diving into the FlatBuffers usage in Dart, it should be noted that the [Tutorial](@ref flatbuffers_guide_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](@ref
flatbuffers_guide_building) documentation to build
and should be familiar with [Using the schema compiler](@ref
flatbuffers_guide_using_schema_compiler) and [Writing a schema](@ref
The code for the FlatBuffers Dart library can be found at
flatbuffers/dart. You can browse the library code on the FlatBuffers
The code to test the Dart library can be found at
flatbuffers/tests. The test code itself is located in
To run the tests, use the [DartTest.sh](https://github.com/google/flatbuffers/ blob/master/tests/DartTest.sh) shell script.
Note: The shell script requires the Dart SDK to be installed.
Note: See [Tutorial](@ref flatbuffers_guide_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
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
import 'dart:io' as io; import 'package:flat_buffers/flat_buffers.dart' as fb; import './monster_my_game.sample_generated.dart' as myGame; List<int> data = await new io.File('monster.dat').readAsBytes(); var monster = new myGame.Monster(data);
Now you can access values like this:
var hp = monster.hp; var pos = monster.pos;
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.
// Deserialize from buffer into object. MonsterT monster = Monster(flatbuffer).unpack(); // Update object directly like a Dart class instance. print(monster.Name); monster.Name = "Bob"; // Change the name. // Serialize into new flatbuffer. final fbb = Builder(); fbb.Finish(monster.pack(fbb));