Error while using non-default type with TypeConverter in Floor entity

91 views Asked by At

I am stuck with a simple error when using a type other than the default type in my entity class. The error occurs when I run the dart run build_runner build --delete-conflicting-outputs command to generate my appdatabase.g.dart file:

[SEVERE] floor_generator on lib/database/AppDatabase.dart:
Column type is not supported for AgroData?'

I followed the procedure for associating a non-default type with an entity by following this documentation

Here are my files :

Parcel.dart : the file where I try to use the custom object "AgroData" with TypeConverters

part 'Parcel.g.dart';

@Entity(primaryKeys: ['localId'])
@JsonSerializable()
class Parcel extends CustomEntity {

  String? name;
  String? exploitationName;
  String? forfaits;

  @TypeConverters([AgroDataConverter])
  final AgroData? agroData;

  String? cultivation;
  String? varietyName;
  double? surface;
  int? sowingDate;
  String? geoDataUrl;

  //local
  int nbOfStages;

  // init
  Parcel({
    required this.name,
    required this.exploitationName,
    required this.forfaits,
    required this.agroData,
    required this.cultivation,
    required this.varietyName,
    required this.surface,
    required this.sowingDate,
    required this.nbOfStages,
    required this.geoDataUrl,
    required int? localId,
    required String? serverId,
    required int creationDate,
    required int lastModifDate,
  }) : super(localId, serverId, creationDate, lastModifDate);
}

AgroData.dart : The file of the custom entity I tried to convert

part 'AgroData.g.dart';

@Entity(primaryKeys: ['localId'])
@JsonSerializable()
class AgroData extends CustomEntity {

  @JsonKey(name: "culturePrecedente")
  String? previousCulture;

  int? createdAt;

  int? updatedAt;

  // init
  AgroData({
    required int? localId,
    required String? serverId,
    required int creationDate,
    required int lastModifDate,
    this.previousCulture,
    this.createdAt,
    this.updatedAt,
  }) : super(localId, serverId, creationDate, lastModifDate);

  factory AgroData.fromJson(Map<String, dynamic> json) => _$AgroDataFromJson(json);

  Map<String, dynamic> toJson() => _$AgroDataToJson(this);
}

CustomEntity.dart : The class inherited by AgroData

part 'CustomEntity.g.dart';

@JsonSerializable()
class CustomEntity {

  @PrimaryKey(autoGenerate: true)
  int? localId;

  @JsonKey(name: "_id")
  String? serverId;

  int creationDate;

  int lastModifDate;

  CustomEntity(this.localId, this.serverId, this.creationDate, this.lastModifDate);

  factory CustomEntity.fromJson(Map<String, dynamic> json) => _$CustomEntityFromJson(json);

  Map<String, dynamic> toJson() => _$CustomEntityToJson(this);
}

AgroDataConverter.dart : The converter of AgroData, used with annotation on Parcel.dart to convert the type

class AgroDataConverter extends TypeConverter<AgroData, String> {

  @override
  AgroData decode(String databaseValue) {
    return AgroData.fromJson(jsonDecode(databaseValue) as Map<String, dynamic>);
  }

  @override
  String encode(AgroData value) {
    return jsonEncode(value.toJson());
  }
}

I'm sure I'm following the documentation, but the error persists. I'd like to point out that although the TypeConverters annotation is at the entity attribute level, I've also tried to put it at the entity level itself and also at the level of my AppDatabase.dart file, but the error persists.

1

There are 1 answers

0
user18954506 On BEST ANSWER

Have you tried to cancel the null safety for the AgroData (i.e., changing final AgroData? agroData; to final AgroData agroData; in the Parcel.dart file ?

Maybe the plugin doesn't support nullable to convert in database ?