Files
syncrow-app/lib/features/app_layout/model/space_model.dart
hannathkadher d025da6daf fixed space
2024-10-30 09:56:57 +04:00

47 lines
1.4 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) {
final spaceData = json['space'] as Map<String, dynamic>? ?? {};
return SpaceModel(
id: json['uuid'] ?? '',
name: spaceData['spaceName'] ?? 'Unnamed Space',
community: Community.fromJson(spaceData['community'] ?? {}),
subspaces: (spaceData['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) => SpaceModel.fromJson(item)).toList();
}
}