1

I have dart web app using sdk 1.24 and have been using dson: 0.11.0 to generate serializable classes/models for my objects that are being saved to a firestore database.

I love the way the dson generated classes give me the ability to create a dart object from a map, or serialize a dart object to a map for saving in firebase.

With that said, the dson generator requires my model class to extend a serializable abstract generated class.

My application is starting to grow rather large and I am struggling with the inability to use inheritance and develop a class hierarchy.

Unless I am missing some concept that I can't seem to crack on my own, I cannot figure out how to use class inheritance with the dson generator.

For example, here is a really simple example of what I am trying to do.

class EmploymentIncome extends Object {        
    String employerName;
    Address employerAddress;
    DateTime hireDate;
}


class SalaryIncome extends EmploymentIncome {
    double annualSalary;
}


class HourlyIncome extends EmploymentIncome {
    double hourlyRate;
    double hoursPerWeek;
 }


class hourlyPaystub extends HourlyIncome {
    double yearToDateHourlyEarnings;
    double hoursWorked;
    DateTime payDate;
    DateTime periodEndingDate;
}


class salaryPaystub extends SalaryIncome {
    double yearToDateSalaryEarnings;
    DateTime payDate;
    DateTime periodEndingDate;
}

The problem is, the dson generator requires my models to extend the generated abstract class, see below:

@serializable
class EmploymentIncome extends _$EmploymentIncomeSerializable {
    class EmploymentIncome extends Object {
    String employerName;
    Address employerAddress;
    DateTime hireDate;
}

The problem, obviously, is now, I cannot extend EmploymentIncome with another serializable dson class.

Am I missing a fundamental concept or technique that would allow me to extend these classes while still maintaining the ability to convert dart objects to and from maps?

Thank you in advance for any guidance!

Jerry
  • 35
  • 2

2 Answers2

0

Two things need to be done:

  • extend generated serializable class using with operator
  • add comment to ignore analysis error

For example in your case you can do:

@serializable
class EmploymentIncome extends _$EmploymentIncomeSerializable {
    String employerName;
    Address employerAddress;
    DateTime hireDate;
}

@serializable
// ignore: mixin_inherits_from_not_object
class SalaryIncome extends EmploymentIncome with _$SalaryIncomeSerializable {
    double annualSalary;
}

@serializable
// ignore: mixin_inherits_from_not_object
class HourlyIncome extends EmploymentIncome with _$HourlyIncomeSerializable {
    double hourlyRate;
    double hoursPerWeek;
 }

@serializable
// ignore: mixin_inherits_from_not_object
class HourlyPaystub extends HourlyIncome with _$HourlyPaySerializable {
    double yearToDateHourlyEarnings;
    double hoursWorked;
    DateTime payDate;
    DateTime periodEndingDate;
}

@serializable
// ignore: mixin_inherits_from_not_object
class SalaryPaystub extends SalaryIncome with _$SalaryPayStubSerializable {
    double yearToDateSalaryEarnings;
    DateTime payDate;
    DateTime periodEndingDate;
}
Luis Vargas
  • 2,466
  • 2
  • 15
  • 32
  • Thank you so much, this was exactly what I was looking for! It also seems the docs have been updated to address this, or else I just didn't understand this when I was looking at it the docs initially. Thank you again for your guidance, again this is EXACTLY what I was looking for....life just got ALOT easier(minus the fact that i now need to/get to rewrite all of my classes )! – Jerry Jun 08 '18 at 21:20
  • Actually, I spoke too soon.... it works in the editor, but I get the following when I try to debug: 'Class used as mixin must have Object as superclass. abstract class _$SalaryIncomeCalcSerializable extends SerializableMap { ^^^^^^^^` – Jerry Jun 10 '18 at 01:18
  • I tested it with the latest version of dart-sdk and it works as expected – Luis Vargas Jun 26 '18 at 18:45
-1

I'm not really familiar with package:dson, but having used and written various serialization code generators, it all boils down to knowing: What class should be created given the input?

In most cases the serialized format does not contain the class information (it is too verbose to store it), and the library can't really decide whether the superclass or the subclass needs to be instantiated for a given input. I suspect that this is the case for dson too.

In most cases you will have better performance and you can remain future-proof if you use composition instead of inheritance. Check out the answer for a similar questions with protobuf.

István
  • 518
  • 1
  • 5
  • 17