Type Converters
Isar only supports storing basic types like numbers and lists but you can use type converters to use other types like Enums with Isar.
TypeConverter
#
Creating a Let's assume we want to store the following enum in Isar:
Writing a converter is easy, just map the enum to a supported type. For enums it is recommended to use the enum index for mapping.
As you can see, just two methods are required. fromIsar()
converts the database value to the Dart representation and toIsar()
does the opposite.
TypeConverter
#
Using a Once you created a type adapter, the hardest part is done. Using TypeAdapters
is super easy. Just annotate the fields you want to convert and you're done.
Now let's try to query all the people in our database that are married:
There is only one very important thing to keep in mind: You may change the converter for an existing field but the database type MUST BE THE SAME. Otherwise the schema migration will fail.