mirror of
https://github.com/SyncrowIOT/syncrow-app.git
synced 2025-07-15 01:35:23 +00:00
Refactor Action class constructor to handle optional fields more efficiently
This commit is contained in:
@ -90,44 +90,25 @@ class Action {
|
|||||||
String toRawJson() => json.encode(toJson());
|
String toRawJson() => json.encode(toJson());
|
||||||
|
|
||||||
static Action? fromJson(Map<String, dynamic> json) {
|
static Action? fromJson(Map<String, dynamic> json) {
|
||||||
// Safely extract required fields with null checks
|
|
||||||
final String? actionExecutor = json["actionExecutor"] as String?;
|
final String? actionExecutor = json["actionExecutor"] as String?;
|
||||||
final String? entityId = json["entityId"] as String?;
|
final String? entityId = json["entityId"] as String?;
|
||||||
final String? productType = json['productType'] as String?;
|
|
||||||
final String? deviceName = json['deviceName'] as String?;
|
|
||||||
|
|
||||||
// Skip invalid actions with missing required fields
|
|
||||||
if (actionExecutor == null ||
|
if (actionExecutor == null || entityId == null) {
|
||||||
entityId == null ||
|
|
||||||
productType == null ||
|
|
||||||
deviceName == null) {
|
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
|
|
||||||
// Handle actions with 'name' and 'type'
|
return Action(
|
||||||
if (json['name'] != null && json['type'] != null) {
|
actionExecutor: actionExecutor,
|
||||||
return Action(
|
entityId: entityId,
|
||||||
actionExecutor: actionExecutor,
|
executorProperty: json["executorProperty"] != null
|
||||||
entityId: entityId,
|
? ExecutorProperty.fromJson(json["executorProperty"])
|
||||||
name: json['name'] as String?,
|
: null,
|
||||||
type: json['type'] as String?,
|
name: json['name'] as String?,
|
||||||
productType: productType,
|
type: json['type'] as String?,
|
||||||
deviceName: deviceName,
|
productType: json['productType'] as String? ?? 'unknown',
|
||||||
);
|
deviceName: json['deviceName'] as String? ?? 'Delay Action',
|
||||||
}
|
);
|
||||||
|
|
||||||
// Handle actions with 'executorProperty'
|
|
||||||
if (json["executorProperty"] != null) {
|
|
||||||
return Action(
|
|
||||||
actionExecutor: actionExecutor,
|
|
||||||
entityId: entityId,
|
|
||||||
executorProperty: ExecutorProperty.fromJson(json["executorProperty"]),
|
|
||||||
productType: productType,
|
|
||||||
deviceName: deviceName,
|
|
||||||
);
|
|
||||||
}
|
|
||||||
|
|
||||||
return null; // Skip invalid actions
|
|
||||||
}
|
}
|
||||||
|
|
||||||
Map<String, dynamic> toJson() => {
|
Map<String, dynamic> toJson() => {
|
||||||
|
Reference in New Issue
Block a user