I have a simple table, which I would like to create and maintain via MOOR. Below is the table definition:
class Plants extends Table {
IntColumn get id => integer().autoIncrement()();
TextColumn get name => text()();
TextColumn get image => text().map(const ImageConverter()).nullable()();
}
As you can see, I have an image
field, and I would like to use Image
to base64
conversion and back. Please, note I refer to Flutter Image class, not the widget.
I already found the topic, where conversion between Image widget and base64 took place, but it does not seem to me right from an architectural point of view to use widget on the data layer.
This is what I have done so far:
import 'package:image/image.dart';
import 'dart:convert';
import 'dart:typed_data';
import 'package:moor/moor.dart';
class ImageConverter extends TypeConverter<Image, String> {
Image _imageFromBase64String(String base64String) {
return null; // <--- missing implementation
}
Uint8List _dataFromBase64String(String base64String) {
return base64Decode(base64String);
}
String _base64String(Uint8List data) {
return base64Encode(data);
}
const ImageConverter();
@override
Image mapToDart(String fromDb) {
if (fromDb == null) {
return null;
}
return _imageFromBase64String(fromDb);
}
@override
String mapToSql(Image value) {
if (value == null) {
return null;
}
return _base64String(value.getBytes());
}
}
As you can see, I have no problem (I guess) to implement Image
to base64
conversion, I struggle however to implement base64
to Image
.