isar
isar copied to clipboard
Support TypeConverter
Thanks for the excellent product!
Is your feature request related to a problem? Please describe. I have a few "TypeConverter"s in version 2.5, how can I migrate them (both the data and code)?


Describe alternatives you've considered Any solution with "Embedded"?
Version
- Platform: iOS, Android, Mac, Windows, Linux, Web
- Flutter version: 3.3.4
- Isar version: 3.0.2
Anyone can help? Thanks!
We would also like to see a solution for migrating type converters. For now we have been using @embedded to replace type converters, but they have some limitations that the old TypeConverter design did not have.
We would also like to see a solution for migrating type converters. For now we have been using
@embeddedto replace type converters, but they have some limitations that the oldTypeConverterdesign did not have.
@KonstantinRr
Could you post some code about your @embedded implementations for reference, thanks!
It is basically the same as here https://isar.dev/schema.html#embedded-objects.
You can build similar things as type encoders around it by storing a String in an embedded object. For example:
@embedded
class JSONBlob {
String? data;
JSONBlob({this.data});
JSONBlob.fromJson(Map<String, dynamic> data) : data = jsonEncode(data);
@Ignore()
Map<String, dynamic>? get json => jsonDecode(data);
}
You can then store data any JSON serializable object by JSONBlob.fromJson({'test': 'hello'}) and retrieve it by calling .json.
It is basically the same as here https://isar.dev/schema.html#embedded-objects. You can build similar things as type encoders around it by storing a
Stringin an embedded object. For example:@embedded class JSONBlob { String? data; JSONBlob({this.data}); JSONBlob.fromJson(Map<String, dynamic> data) : data = jsonEncode(data); @Ignore() Map<String, dynamic>? get json => jsonDecode(data); }You can then store data any JSON serializable object by
JSONBlob.fromJson({'test': 'hello'})and retrieve it by calling.json.
Thanks for your code~
In this way I've to rewrite all old properties. eg.UserModel to new @embeded model IsarUserModel.
Type converters have been removed on purpose. You should not store objects in Isar that are unsupported. For json you should just store it as String.
@simc
I don't understand why TypeConverters were removed. Why can't they work in parallel with the new system? For some data @embedded doesn't just work without much boilerblate. For example, I want to store a Color and had a TypeConverter for that, which simply stores the Color as String. This does not work now. To be able to store a Color now, you would have to write your own Color class, which is unnecessary overhead.
So what is the recommended approach when you want to store objects that are not in your own library?