You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
cloudsolutions-atoms/lib/models/new_models/building.dart

63 lines
1.4 KiB
Dart

import 'dart:developer';
import 'package:test_sa/models/base.dart';
import 'package:test_sa/models/new_models/floor.dart';
class Building extends Base {
Building({
this.id,
this.value,
this.floors,
String? name,
}) : super(identifier: id?.toString() ?? '', name: name); // Handle potential null id
Building.fromJson(dynamic json) {
id = json['id'] ?? json['buildingId'];
identifier = id?.toString() ?? '';
name = json['name'] ?? json['buildingName'];
value = json['value'];
if (json['floors'] != null) {
floors = [];
json['floors'].forEach((v) {
floors!.add(Floor.fromJson(v));
});
} else if (json['assetInventoryFloors'] != null) {
floors = [];
json['assetInventoryFloors'].forEach((v) {
floors!.add(Floor.fromJson(v));
});
}
}
num? id;
num? value;
List<Floor>? floors;
Building copyWith({
num? id,
String? name,
num? value,
List<Floor>? floors,
}) =>
Building(
id: id ?? this.id,
name: name ?? this.name,
value: value ?? this.value,
floors: floors ?? this.floors,
);
Map<String, dynamic> toJson({bool addFloor = true}) {
final map = <String, dynamic>{};
map['id'] = id;
map['name'] = name;
map['value'] = value;
if (addFloor) {
if (floors != null) {
map['floors'] = floors!.map((v) => v.toJson()).toList();
}
}
return map;
}
}