Skip to content

Bug: AlarmModel.fromJson does not initialize fields #867

@dorodb-web22

Description

@dorodb-web22

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 LateInitializationError

The 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.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions