Files
syncrow-app/lib/features/app_layout/model/space_model.dart
2024-10-30 11:04:36 +04:00

54 lines
1.6 KiB
Dart

import 'package:syncrow_app/features/app_layout/model/community_model.dart';
import 'package:syncrow_app/features/devices/model/subspace_model.dart';
class SpaceModel {
final String id;
final String name;
final Community community;
late List<SubSpaceModel> subspaces;
SpaceModel({
required this.id,
required this.name,
required this.community,
this.subspaces = const [], // Default to an empty list
});
/// Converts the instance into JSON format.
Map<String, dynamic> toJson() {
return {
'id': id,
'spaceName': name,
'community': community.toJson(),
'subspaces': subspaces.map((room) => room.toJson()).toList(),
};
}
/// Factory constructor to create an instance from JSON.
factory SpaceModel.fromJson(Map<String, dynamic> json) {
// Extract and log each part of space data
final id = json['uuid'] ?? '';
final name = json['spaceName'] ?? 'Unnamed Space';
final communityJson = json['community'] ?? {};
return SpaceModel(
id: id,
name: name,
community: Community.fromJson(
communityJson), // Ensure Community is created correctly
subspaces: (json['subspaces'] as List<dynamic>?)
?.map((item) => SubSpaceModel.fromJson(item))
.toList() ??
[],
);
}
/// Helper method to parse a list of SpaceModel from JSON.
static List<SpaceModel> fromJsonList(List<dynamic> jsonList) {
return jsonList.map((item) {
final spaceData = item['space']; // Extract the `space` object
return SpaceModel.fromJson(spaceData); // Pass to SpaceModel.fromJson
}).toList();
}
}