fromJson static method

HDPrinterDevice? fromJson(
  1. Map<Object?, Object?> json
)

Rebuilds a device from the {provider, address, name} map the channel carries. Returns null for a shape the two sides disagree on.

Implementation

static HDPrinterDevice? fromJson(Map<Object?, Object?> json) {
  final providerName = json['provider'] as String?;
  final address = json['address'] as String?;
  if (providerName == null || address == null || address.isEmpty) return null;
  PrinterProvider? provider;
  for (final p in PrinterProvider.values) {
    if (p.name.toUpperCase() == providerName.toUpperCase()) {
      provider = p;
      break;
    }
  }
  if (provider == null) return null;
  return HDPrinterDevice(
    provider: provider,
    address: address,
    name: (json['name'] as String?)?.isNotEmpty == true
        ? json['name'] as String
        : address,
  );
}