'Flutter store and retrieve date as YYYMM. Is Model Class the right place to do it?

For my Flutter application, I need the 'date' to be stored in Firestore as either 'YYYYMM' or 'YYYYMMDD' (Number) instead of as a Firestore Timestamp. However, to use the various date widgets, I may have to convert them into a Timestamp object to use DatePicker, etc.

So should I do this conversion in the Model Class or in the helper method that does the writing to the Firebase Firestore?



Solution 1:[1]

You mean you need to use the DateTime class instead of the Timestamp class?

I would use the model class. In this class define the data type for date as a Number. Create a setter method like this:

...
Number dateNumber;
...
void set date(DateTime inDate) {
  // put your conversion algorithm here
  dateNumber = ...
}

Later:

data.date = DateTime.now(); // for example

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1