-
Notifications
You must be signed in to change notification settings - Fork 224
Description
While reviewing AlarmModel,
i noticed that the fromJson constructor calls AlarmModel.fromMap(...) but does not use the returned object.
AlarmModel.fromJson(String alarmData, UserModel? user) {
AlarmModel.fromMap(jsonDecode(alarmData));
}This creates a new AlarmModel instance and immediately discards it.
The current instance (this) is never initialized.
As a result, all late fields in AlarmModel remain unset.
Affected File
lib/app/data/models/alarm_model.dart (lines 350–352)
Why this is a problem
Since most fields in AlarmModel are declared as late, accessing any of them on an object created using fromJson will throw:
LateInitializationError: Field '<fieldName>' has not been initialized.Example:-
final alarm = AlarmModel.fromJson(jsonString, null);
print(alarm.alarmID); // throws LateInitializationErrorThe object exists, but none of its fields have been populated.
Current impact
This does not currently break production code because fromJson does not appear to be used in active code paths.
However, since it is publicly available on the model, using it in the future (e.g., for notification payloads or restore flows) would result in invalid objects and runtime crashes.
Suggested Fix
Convert fromJson into a redirecting constructor:
AlarmModel.fromJson(String alarmData, UserModel? user)
: this.fromMap(jsonDecode(alarmData) as Map<String, dynamic>);Or alternatively, make it a factory:
factory AlarmModel.fromJson(String alarmData, UserModel? user) {
return AlarmModel.fromMap(
jsonDecode(alarmData) as Map<String, dynamic>);
}This keeps the behavior consistent with fromMap and ensures the instance is properly initialized.