Flutter Floor Database. Failed to generate the floor database

276 Views Asked by At

I followed the floor documentation step by step. Entities, Dao, Converters and database have been created. Build_generator generated the model.g.dart files type but not the database.g.dart file.

I encounter two errors:

Could not generate `fromJson` code for `changeListener`.
To support the type `StreamController` you can:
* Use `JsonConverter`
https://pub.dev/documentation/json_annotation/latest/json_annotation/JsonConverter-class.html
* Use `JsonKey` fields `fromJson` and `toJson`
https://pub.dev/documentation/json_annotation/latest/json_annotation/JsonKey/fromJson.html
https://pub.dev/documentation/json_annotation/latest/json_annotation/JsonKey/toJson.html
package:floor/src/database.dart:11:39
  ╷
11 │   late final StreamController<String> changeListener;

Error that I find weird because it asks me to create a converter for a listener of the FloorDatabase class...

And also:

There are no entities added to the database annotation. package:sirusmobile/database/database.dart:93:16 ╷ 93 │ abstract class AppDatabase extends FloorDatabase {

However, the entities are well informed.

part 'database.g.dart';

@JsonSerializable()
@Database(version: 2, entities: [
  AccesToken,
  Account,
  Chauffeur,
  Coordonnees,
  FilsDiscussion,
  LieuIntervention,
  Mission,
  Permanence,
  PositionGps,
  Secteur,
  Societe,
  Transmission,
  Vehicule,
  Delai,
  Destination,
  Motifs,
  Raison,
  Synchro
])
@TypeConverters([
  StringListConverter,
  ChauffeurConverter,
  DegresUrgenceConverter,
  DelaiConverter,
  DestinationConverter,
  EquipementConverter,
  FilsDiscussionConverter,
  MotifsAnnulationConverter,
  MotifsRefusConverter,
  MotifsSortieBlancheConverter,
  RaisonConverter,
  SocieteConverter,
  VehiculeConverter
])
abstract class AppDatabase extends FloorDatabase {
  AccesTokenDao get accesTokenDao;

  AccountDao get accountTokenDao;

  ChauffeurDao get chauffeurDao;

  CoordonneesDao get coordoneesDao;

  FilsDiscussionDao get filsDiscussionDao;

  LieuInterventionDao get lieuInterventionDao;

  MissionDao get missionDao;

  PermanenceDao get permanenceDao;

  PositionGpsDao get positionGps;

  SecteurDao get secteurDao;

  SocieteDao get societeDao;

  TransmissionDao get transmissionDao;

  VehiculeDao get vehiculeDao;

  DelaiDao get delaiDao;

  DestinationDao get destinationDao;

  MotifsDao get motifsDao;

  RaisonDao get raisonDao;

  SynchroDao get synchroDao;
}

I've been trying to solve this problem for too long. Could someone help me please? Thanks in advance.

2

There are 2 best solutions below

0
Gaël CHEYROUSE On BEST ANSWER

I'm answering my question because I solved the problem. I did two things for this.

  • In the DAOs the names of the classes in the queries were not correct ones.

  • In the entities I did not have the fromMap and toMap methods but only the json methods fromJson and toJson.

Hope this can help some people.

Good day !

0
Gaël CHEYROUSE On

I tried to comment out the @JsonSerializable() annotation in the AppDatabase class and the error regarding the changeListener disappears.

There is still the second one:

There are no entities added to the database annotation.

However the models seem ok.

For example :

part 'Transmission.g.dart';

@JsonSerializable(includeIfNull: false)
@entity
class Transmission {
  @primaryKey
  int? idPrim;
  int? id;
  String? codeSociete;
  String? codeVehicule;
  int? delaiCalcule;
  bool? negocie;
  int? delaiNegocie;
  int? delaiMax;
  String? etat;

  Transmission(
      {this.id,
        this.codeSociete,
        this.codeVehicule,
        this.delaiCalcule,
        this.negocie,
        this.delaiNegocie,
        this.delaiMax,
        this.etat});

  Transmission.fromJson(Map<String, dynamic> json) {
    id = json['id'];
    codeSociete = json['codeSociete'];
    codeVehicule = json['codeVehicule'];
    delaiCalcule = json['delaiCalcule'];
    negocie = json['negocie'];
    delaiNegocie = json['delaiNegocie'];
    delaiMax = json['delaiMax'];
    etat = json['etat'];
  }

  Map<String, dynamic> toJson() {
    final Map<String, dynamic> data = new Map<String, dynamic>();
    data['id'] = this.id;
    data['codeSociete'] = this.codeSociete;
    data['codeVehicule'] = this.codeVehicule;
    data['delaiCalcule'] = this.delaiCalcule;
    data['negocie'] = this.negocie;
    data['delaiNegocie'] = this.delaiNegocie;
    data['delaiMax'] = this.delaiMax;
    data['etat'] = this.etat;
    return data;
  }
}

Entity imports into the AppDatabase class also seem ok.

import 'package:sirusmobile/models/transmission.dart';