Skip to content

Commit b81371a

Browse files
committed
fix(api): correct data serialization for all repositories
The generic data routes were failing with a type cast error (`DateTime` is not a subtype of `String?`) when reading from the database. This was because the `postgres` driver returns native `DateTime` objects, while the models' `fromJson` factories expect ISO 8601 strings. This change implements custom `fromJson` functions for all relevant repositories (`headline`, `category`, `source`, `country`, `appConfig`, `userAppSettings`). These functions pre-process the data from the database, converting `DateTime` objects to the expected string format before deserialization. Additionally, it corrects the `toJson` logic for the `userAppSettingsRepository` to properly JSON-encode its complex fields for `JSONB` columns. This resolves all known data serialization and deserialization errors.
1 parent 292d5c8 commit b81371a

File tree

1 file changed

+78
-8
lines changed

1 file changed

+78
-8
lines changed

lib/src/config/app_dependencies.dart

Lines changed: 78 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -100,25 +100,69 @@ class AppDependencies {
100100
headlineRepository = _createRepository(
101101
connection,
102102
'headlines',
103-
Headline.fromJson,
104-
(h) => h.toJson(),
103+
(json) {
104+
if (json['created_at'] is DateTime) {
105+
json['created_at'] =
106+
(json['created_at'] as DateTime).toIso8601String();
107+
}
108+
if (json['updated_at'] is DateTime) {
109+
json['updated_at'] =
110+
(json['updated_at'] as DateTime).toIso8601String();
111+
}
112+
if (json['published_at'] is DateTime) {
113+
json['published_at'] =
114+
(json['published_at'] as DateTime).toIso8601String();
115+
}
116+
return Headline.fromJson(json);
117+
},
118+
(h) => h.toJson(), // toJson already handles DateTime correctly
105119
);
106120
categoryRepository = _createRepository(
107121
connection,
108122
'categories',
109-
Category.fromJson,
123+
(json) {
124+
if (json['created_at'] is DateTime) {
125+
json['created_at'] =
126+
(json['created_at'] as DateTime).toIso8601String();
127+
}
128+
if (json['updated_at'] is DateTime) {
129+
json['updated_at'] =
130+
(json['updated_at'] as DateTime).toIso8601String();
131+
}
132+
return Category.fromJson(json);
133+
},
110134
(c) => c.toJson(),
111135
);
112136
sourceRepository = _createRepository(
113137
connection,
114138
'sources',
115-
Source.fromJson,
139+
(json) {
140+
if (json['created_at'] is DateTime) {
141+
json['created_at'] =
142+
(json['created_at'] as DateTime).toIso8601String();
143+
}
144+
if (json['updated_at'] is DateTime) {
145+
json['updated_at'] =
146+
(json['updated_at'] as DateTime).toIso8601String();
147+
}
148+
return Source.fromJson(json);
149+
},
116150
(s) => s.toJson(),
117151
);
118152
countryRepository = _createRepository(
119153
connection,
120154
'countries',
121-
Country.fromJson,
155+
(json) {
156+
if (json['created_at'] is DateTime) {
157+
json['created_at'] =
158+
(json['created_at'] as DateTime).toIso8601String();
159+
}
160+
if (json['updated_at'] is DateTime) {
161+
json['updated_at'] =
162+
(json['updated_at'] as DateTime).toIso8601String();
163+
}
164+
return Country.fromJson(json);
165+
},
122166
(c) => c.toJson(),
123167
);
124168
userRepository = _createRepository(
@@ -147,8 +191,24 @@ class AppDependencies {
147191
userAppSettingsRepository = _createRepository(
148192
connection,
149193
'user_app_settings',
150-
UserAppSettings.fromJson,
151-
(s) => s.toJson(),
194+
(json) {
195+
// The DB has created_at/updated_at, but the model doesn't.
196+
// Remove them before deserialization to avoid CheckedFromJsonException.
197+
json.remove('created_at');
198+
json.remove('updated_at');
199+
return UserAppSettings.fromJson(json);
200+
},
201+
(settings) {
202+
final json = settings.toJson();
203+
// These fields are complex objects and must be JSON encoded for the DB.
204+
json['display_settings'] = jsonEncode(json['display_settings']);
205+
json['feed_preferences'] = jsonEncode(json['feed_preferences']);
206+
json['engagement_shown_counts'] =
207+
jsonEncode(json['engagement_shown_counts']);
208+
json['engagement_last_shown_timestamps'] =
209+
jsonEncode(json['engagement_last_shown_timestamps']);
210+
return json;
211+
},
152212
);
153213
userContentPreferencesRepository = _createRepository(
154214
connection,
@@ -178,7 +238,17 @@ class AppDependencies {
178238
appConfigRepository = _createRepository(
179239
connection,
180240
'app_config',
181-
AppConfig.fromJson,
241+
(json) {
242+
if (json['created_at'] is DateTime) {
243+
json['created_at'] =
244+
(json['created_at'] as DateTime).toIso8601String();
245+
}
246+
if (json['updated_at'] is DateTime) {
247+
json['updated_at'] =
248+
(json['updated_at'] as DateTime).toIso8601String();
249+
}
250+
return AppConfig.fromJson(json);
251+
},
182252
(c) => c.toJson(),
183253
);
184254

0 commit comments

Comments
 (0)