This is the solution I finally implemented in Flutter using pgtype.Date and pgtype.DateTimestamptz in my Go api and column types DATE and DATETIME in Postgresql. I was unable to use a pointer with this data type.
First I created a helper in Flutter like this:
import 'package:intl/date_symbol_data_local.dart';
import 'package:intl/intl.dart';
extension DateTimeExtension on DateTime {
String format([String pattern = 'dd/MM/yyyy', String? locale]) {
if (locale != null && locale.isNotEmpty) {
initializeDateFormatting(locale);
}
return DateFormat(pattern, locale).format(this);
}
String formatDbDateTime(
[String pattern = "yyyy-MM-dd'T'HH:mm:ss.SSS'Z'", String? locale]) {
if (locale != null && locale.isNotEmpty) {
initializeDateFormatting(locale);
}
return DateFormat(pattern, locale).format(this);
}
String formatDbDate([String pattern = 'yyyy-MM-dd', String? locale]) {
if (locale != null && locale.isNotEmpty) {
initializeDateFormatting(locale);
}
return DateFormat(pattern, locale).format(this);
}
String formatLocalDate([String pattern = 'dd MMMM yyyy', String? locale]) {
if (locale != null && locale.isNotEmpty) {
initializeDateFormatting(locale);
}
return DateFormat(pattern, locale).format(this);
}
String formatLocalDateTime(
[String pattern = 'dd MMMM yyyy h:mm a', String? locale]) {
if (locale != null && locale.isNotEmpty) {
initializeDateFormatting(locale);
}
return DateFormat(pattern, locale).format(this);
}
}
I used this in my Flutter models to format the date for my Go Api:
if (planAcceptedDate != null) {
data['plan_accepted_date'] = planAcceptedDate!.formatDbDateTime();
} else {
data['plan_accepted_date'] = null;
}
if (invoiceDate != null) {
data['invoice_date'] = invoiceDate!.formatDbDate();
} else {
data['invoice_date'] = null;
}
and in my Flutter view form I use it like this:
Container(
padding: EdgeInsets.all(8.0),
child: Text(
_matter.planAcceptedDate == null
? ''
: _matter.planAcceptedDate!.formatLocalDateTime(),
style: TextStyle(
color: appTextColor,
fontWeight: FontWeight.normal,
fontSize: _user.fontsize,
),
),
),
Container(
padding: EdgeInsets.all(8.0),
child: Text(
_matter.invoiceDate == null
? ''
: _matter.invoiceDate!.formatLocalDate(),
style: TextStyle(
color: appTextColor,
fontWeight: FontWeight.normal,
fontSize: _user.fontsize,
),
),
),
My Go struct looks like this:
PlanAcceptedDate pgtype.Timestamptz `json:"plan_accepted_date"`
InvoiceDate pgtype.Date `json:"invoice_date"`