import 'package:freezed_annotation/freezed_annotation.dart'; part 'device.freezed.dart'; part 'device.g.dart'; /// API 有时返回完整对象,有时只返回整数 ID,需要容错 class _SafeDeviceTypeConverter implements JsonConverter { const _SafeDeviceTypeConverter(); @override DeviceType? fromJson(Object? json) { if (json is Map) { return DeviceType.fromJson(json); } return null; // 整数 ID 或 null → 忽略 } @override Object? toJson(DeviceType? object) => object?.toJson(); } @freezed abstract class DeviceType with _$DeviceType { const factory DeviceType({ required int id, required String brand, required String productCode, required String name, @Default(true) bool isNetworkRequired, @Default(true) bool isActive, String? createdAt, }) = _DeviceType; factory DeviceType.fromJson(Map json) => _$DeviceTypeFromJson(json); } @freezed abstract class DeviceInfo with _$DeviceInfo { const factory DeviceInfo({ required int id, required String sn, @_SafeDeviceTypeConverter() DeviceType? deviceType, @_SafeDeviceTypeConverter() DeviceType? deviceTypeInfo, String? macAddress, @Default('') String name, @Default('in_stock') String status, @Default(false) bool isOnline, @Default('') String firmwareVersion, String? lastOnlineAt, String? createdAt, }) = _DeviceInfo; factory DeviceInfo.fromJson(Map json) => _$DeviceInfoFromJson(json); } @freezed abstract class UserDevice with _$UserDevice { const factory UserDevice({ required int id, required DeviceInfo device, int? spirit, String? spiritName, @Default('owner') String bindType, String? bindTime, @Default(true) bool isActive, }) = _UserDevice; factory UserDevice.fromJson(Map json) => _$UserDeviceFromJson(json); }